5

This document: Preventing Sensitive Information From Appearing In The Task Switcher describes a way to present a view controller in applicationDidEnterBackground so as to hide critical information in the task switcher:

- (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];
}

Yet, in iOS 8, this exact code does not work, and the very simple, plain, black view controller is not shown until after the app becomes active again. The task switcher shows the sensitive information and nothing is hidden. There are no animations in this code, so I cannot understand - why is this happening?

SAHM
  • 4,078
  • 7
  • 41
  • 77
  • In my case the code kind of works... In many cases I get the "blacked out" effect but it is definitely not working when the app has a modal view presented. – moliveira Jul 27 '16 at 14:32
  • I also get "Warning: Attempt to present on whose view is not in the window hierarchy!" – moliveira Jul 27 '16 at 14:35
  • FYI Fixed my modal view problem by using UIWindow.topMostController() instead of the suggested rootViewController() – moliveira Jul 27 '16 at 15:00

1 Answers1

2

In iOS 8 the app is not being given enough time to display the view controller before the screenshot is taken.

The fix that works for me is to just introduce a small run loop run at the end of 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]];
}
Brent
  • 71
  • 3
  • 1
    I accept this answer with reservation because, while it does work in some circumstances, I think it might be a bit hackish and I could see it breaking down under other circumstances. – SAHM Nov 13 '14 at 20:33
  • This does not fix the problem when the app being put in the background has a modal view presented. – moliveira Jul 27 '16 at 14:36