backgroundios8sensitive-datatask-switching

Preventing Sensitive Information From Appearing In The Task Switcher - Apple Code Not Working - iOS 8 glitch?


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?


Solution

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