延迟applicationDidEnterBackground屏幕捕获

当应用程序进入后台时,有没有办法延迟iOS的屏幕截图? 原因是我有时会显示一个视图,当用户转到主屏幕我想删除这个视图,所以它不显示应用程序恢复时。 有问题的视图是一个SSHUDView (源代码在这里 ),我称之为dismissAnimated:NOapplicationWillResignActive dismissAnimated:NO 。 这实际上在模拟器中工作,但不在真实的设备上(恢复时, SSHUDView仍然显示)。 任何人都有一个线索如何处理这个?

如果你看看SSHUDView实现,它使用UIWindow实例来显示内容; 它使得它自己的UIWindow成为UIWindow的关键:

 [_hudWindow makeKeyAndVisible]; 

这可能是为什么摆脱它是如此棘手。 你可能会尝试做什么SSHUDView之后,它会自动返回焦点到主窗口:

 [[[[UIApplication sharedApplication] windows] objectAtIndex:0] makeKeyWindow]; 

resignKeyWindow视图自行resignKeyWindow ,它将_hudWindow发送给它的私有_hudWindow对象,在将焦点返回到主窗口之前,可以尝试获取它的句柄,如下所示:

 [[[[UIApplication sharedApplication] windows] objectAtIndex:1] resignKeyWindow]; 

但是,这可能会导致意想不到的问题,因为你正在绕过SSHUDView API …

我想你应该在applicationDidEnterBackground:处理这个问题。

根据苹果的文件 :

  • 准备拍照。applicationDidEnterBackground:方法返回时,系统会拍摄应用程序的用户界面的图片,并将结果图像用于过渡animation。 如果接口中的任何视图包含敏感信息,则应在applicationDidEnterBackground:方法返回之前隐藏或修改这些视图。

你可能会得到视图控制器显示SSHUDView从[NSNotificationCenter defaultCenter]侦听UIApplicationWillResignActiveNotification并将其隐藏吗? 似乎有点哈克,所以它可能无法正常工作。 只是一个想法。

这对我来说,无论是模拟器和真正的硬件。 在applicationDidEnterBackground:你可以创build一个黑色背景的临时视图控制器,并在“最顶层”的视图控制器上进行模态显示(如果已经有一个视图控制器被模态显示)。 当应用程序返回到applicationWillEnterForeground:时,只需将其删除applicationWillEnterForeground:

 #import "AppDelegate.h" @interface AppDelegate () @property (strong, nonatomic) UIViewController *veilController; @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.window.rootViewController = [[UIViewController alloc] initWithNibName:nil bundle:nil]; self.window.rootViewController.view.backgroundColor = [UIColor redColor]; [self.window makeKeyAndVisible]; UIViewController *vc0 = [[UIViewController alloc] initWithNibName:nil bundle:nil]; vc0.view.backgroundColor = [UIColor blueColor]; [self.window.rootViewController presentViewController:vc0 animated:NO completion:nil]; UIViewController *vc1 = [[UIViewController alloc] initWithNibName:nil bundle:nil]; vc1.view.backgroundColor = [UIColor purpleColor]; [vc0 presentViewController:vc1 animated:NO completion:nil]; return YES; } - (void)applicationDidEnterBackground:(UIApplication *)application { self.veilController = [[UIViewController alloc] initWithNibName:nil bundle:nil]; self.veilController.view.backgroundColor = [UIColor blackColor]; UIViewController *tvc = self.window.rootViewController; while (tvc) { if (tvc.presentedViewController) { tvc = tvc.presentedViewController; } else { [tvc presentViewController:self.veilController animated:NO completion:nil]; tvc = nil; } } } - (void)applicationWillEnterForeground:(UIApplication *)application { [self.veilController dismissViewControllerAnimated:NO completion:nil]; self.veilController = nil; } @end