Preventing snapshot view of your app when coming b

2019-02-06 11:36发布

问题:

The problem is this - My app lets you passcode protect itself. I use an interface just like passcode protecting the phone. This has always worked fine, until multi-tasking came along.

The passcode protection still works, but there is one issue. Apple does something special to make it look like our apps are loading quicker when they come back from the background. The os takes a picture of our screen just before the user leaves the app, and it displays that while the rest of the app is still loading.

The problem this causes is that someone trying to go to my app would see that image of the screen before the passcode protection kicked in. Granted, it's not much, but I don't think my users will like the idea of people being able to get even a little glimpse of their data.

How to stop that snapshot image from showing?

回答1:

I solved this. Here is the solution:

- (void)applicationDidEnterBackground:(UIApplication *)application{
    if (appHasPasscodeOn){
        UIImageView *splashView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, 320, 480)];
        splashView.image = [UIImage imageNamed:@"Default.png"];
        [window addSubview:splashView];
        [splashView release];
    }
}

Default.png is a screenshot of my app with a blank screen (for me it's just a blank listview). The code above puts that in front of my real view right before the app goes into the background. So, when you come back to the app that is all you see. Voila.



回答2:

The marked answer works perfectly for me except that when the app becomes active again the splashView stays on screen. I just made it a property and added [splashView removeFromSuperview] into my applicationWillEnterForeground to fix it. In case anyone else gets similar behavior.



回答3:

Here is the above solution in Swift 3.0:

lazy var splashImageView: UIImageView = {
    let splashImageView = UIImageView(frame: UIScreen.main.bounds)
    splashImageView.image = UIImage(named: "splash-view")
    return splashImageView
}()

func applicationDidEnterBackground(_ application: UIApplication) {
    window?.addSubview(splashImageView)
}

func applicationWillEnterForeground(_ application: UIApplication) {
   splashImageView.removeFromSuperview()
}