后台 – 防止敏感信息出现在任务切换器中 – Apple代码无法正常工作 – iOS 8故障?

本文档:
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中,这个确切的代码不起作用,并且直到应用程序再次变为活动状态之后才会显示非常简单的普通黑色视图控制器.任务切换器显示敏感信息,不隐藏任何内容.这段代码中没有动画,所以我无法理解 – 为什么会发生这种情况?

最佳答案 在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]];
}
点赞