在applicationDidEnterBackground之前显示一个视图或启animation面(避免活动视图截图)

我的应用程序中有机密信息,因此当应用程序即将移至背景时,我想用启animation面将其隐藏起来。

我确实在iOS6上运行应用程序。

我试图在applicationWillResignActive显示视图,但问题是它甚至在用户滑动控制面板时显示启animation面。 我只希望它显示只有当应用程序被移动到后台。

我试图在applicationDidEnterBackground显示我的splashScreen,但是它在animation之前在恢复时显示信息。

在这里,我想要的精神:

 - (void)applicationDidEnterBackground:(UIApplication *)application { [_window addSubview:__splashController.view]; } 

我认为问题在于你正在模拟器中testing。 在设备上,它应该工作正常。

我testing了这个,它工作。 当应用程序进入后台时添加一个imageview与你的飞溅图像 –

 - (void)applicationDidEnterBackground:(UIApplication *)application { UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.window.bounds]; imageView.tag = 101; // Give some decent tagvalue or keep a reference of imageView in self // imageView.backgroundColor = [UIColor redColor]; [imageView setImage:[UIImage imageNamed:@"Default.png"]]; // assuming Default.png is your splash image's name [UIApplication.sharedApplication.keyWindow.subviews.lastObject addSubview:imageView]; } 

当应用程序回到前台 –

 - (void)applicationWillEnterForeground:(UIApplication *)application { UIImageView *imageView = (UIImageView *)[UIApplication.sharedApplication.keyWindow.subviews.lastObject viewWithTag:101]; // search by the same tag value [imageView removeFromSuperview]; } 

注意 – 在模拟器(iOS 7.0)上,通过按下主页button两次(Cmd + H),添加的子视图不会显示,但在设备上按预期工作(如paypalBofA应用程序)

编辑:(附加信息)

除了通过添加子视图/模糊来隐藏/replace敏感信息外, iOS 7还允许您通过applicationWillResignActiveapplicationDidEnterBackground内的UIApplication ignoreSnapshotOnNextApplicationLaunch来忽略屏幕快照。

UIApplication.h

 // Indicate the application should not use the snapshot on next launch, even if there is a valid state restoration archive. // This should only be called from methods invoked from State Preservation, else it is ignored. - (void)ignoreSnapshotOnNextApplicationLaunch NS_AVAILABLE_IOS(7_0); 

而且,可以在限制有效负载中探索allowScreenShot标志。

有同样的问题,基本上我使用applicationDidEnterBackground在内容之上显示一个UIWindow,但在iOS8中,它不像在iOS7中那样工作。

我find的解决scheme是在applicationWillResignActive创buildUIWindow,但隐藏了securityWindow.hidden = YES ; 然后在applicationDidEnterBackground我会做的就是改变securityWindow.hidden = NO

这似乎与iOS7完全一样,在使用NotificationCenter或ControlPanel时在多任务时隐藏内容而不影响视图。

Swift 3.0回答那些懒惰的人翻译。

 func applicationDidEnterBackground(_ application: UIApplication) { let imageView = UIImageView(frame: self.window!.bounds) imageView.tag = 101 imageView.image = ... UIApplication.shared.keyWindow?.subviews.last?.addSubview(imageView) } func applicationWillEnterForeground(_ application: UIApplication) { if let imageView : UIImageView = UIApplication.shared.keyWindow?.subviews.last?.viewWithTag(101) as? UIImageView { imageView.removeFromSuperview() } } 

不知道为什么,但这里描述的方法都不适合我。 我只是试图掩盖屏幕出于安全原因。 那么,苹果的技术问答有什么帮助: https : //developer.apple.com/library/ios/qa/qa1838/_index.html

我猜主要区别是使用UIViewController? 无论如何,下面的代码完全适合我在IOS 9.1上:

 - (void)applicationDidEnterBackground:(UIApplication *)application { UIViewController *blankViewController = [UIViewController new]; blankViewController.view.backgroundColor = [UIColor blackColor]; [self.window.rootViewController presentViewController:blankViewController animated:NO completion:NULL]; } - (void)applicationWillEnterForeground:(UIApplication *)application { [self.window.rootViewController dismissViewControllerAnimated:NO completion:NO]; } 

需要编写的代码如下:

 -(void)applicationWillResignActive:(UIApplication *)application { imageView = [[UIImageView alloc]initWithFrame:[self.window frame]]; [imageView setImage:[UIImage imageNamed:@"Portrait(768x1024).png"]]; [self.window addSubview:imageView]; } 

这里删除imageview:

 - (void)applicationDidBecomeActive:(UIApplication *)application { if(imageView != nil) { [imageView removeFromSuperview]; imageView = nil; } } 

它正在工作并经过适当的testing。

 @interface MyAppDelegate () @property (strong, nonatomic) MySplashView *splashView; @end @implementation MyAppDelegate - (void)applicationWillResignActive:(UIApplication *)application { // hide keyboard and show a splash view [self.window endEditing:YES]; MySplashView *splashView = [[MySplashView alloc] initWithFrame:self.window.bounds]; [self.window addSubview:splashView]; self.splashView = splashView; } - (void)applicationDidBecomeActive:(UIApplication *)application { // remove the splash view if (self.splashView) { [self.splashView removeFromSuperview]; self.splashView = nil; } } @end 

这固定了我,对不起,这是Xamarin.Forms,但你应该明白了。 您需要在iOS中调用xamarin中的UIView.SnapshotView(true)或iOS中的UIView snapshotViewAfterScreenUpdates 。 在iOS7和iOS8的DidEnterBackground中工作:

 public override void DidEnterBackground(UIApplication uiApplication) { App.Current.MainPage = new DefaultPage(); **UIApplication.SharedApplication.KeyWindow.SnapshotView(true);** base.DidEnterBackground(uiApplication); } 

我认为这将有助于Swift 3.0

 func applicationWillResignActive(_ application: UIApplication) { let imageView = UIImageView(frame: self.window!.bounds) imageView.tag = 101 imageView.backgroundColor = UIColor.white imageView.contentMode = .center imageView.image = #image# UIApplication.shared.keyWindow?.subviews.last?.addSubview(imageView) } func applicationWillEnterForeground(_ application: UIApplication) { ReachabilityManager.shared.stopMonitoring() if let imageView : UIImageView = UIApplication.shared.keyWindow?.subviews.last?.viewWithTag(101) as? UIImageView { imageView.removeFromSuperview() } }