5

本文档:Preventing Sensitive Information From Appearing In The Task Switcher描述了一种在 applicationDidEnterBackground 中显示视图控制器的方法,以便在任务切换器中隐藏关键信息:

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Your application can present a full screen modal view controller to
    // cover its contents when it moves into the background. If your
    // application requires a password unlock when it retuns to the
    // foreground, present your lock screen or authentication view controller here.

    UIViewController *blankViewController = [UIViewController new];
    blankViewController.view.backgroundColor = [UIColor blackColor];

    // Pass NO for the animated parameter. Any animation will not complete
    // before the snapshot is taken.
    [self.window.rootViewController presentViewController:blankViewController animated:NO completion:NULL];
}

然而,在 iOS 8 中,这个确切的代码不起作用,并且非常简单、普通的黑色视图控制器直到应用程序再次激活后才会显示。任务切换器显示敏感信息,没有任何隐藏。这段代码中没有动画,所以我无法理解 - 为什么会这样?

4

1 回答 1

2

在 iOS 8 中,应用程序没有足够的时间在截取屏幕截图之前显示视图控制器。

对我有用的解决方法是在 applicationDidEnterBackground 的末尾引入一个小的运行循环运行。

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    UIViewController *blankViewController = UIViewController.new;
    blankViewController.view.backgroundColor = UIColor.blackColor;
    [self.window.rootViewController presentViewController:blankViewController animated:NO completion:NULL];
    [NSRunLoop.currentRunLoop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.5]];
}
于 2014-11-07T12:56:28.063 回答