问题
I have some sensitive data displayed to my user within the app, and when the user clicks the home button and comes back into the app, Apple has a snapshot functionality that displays a picture of the last screen on the device before the home button was pressed.
Is there any way to disable this effect entirely? I have seen ways here to display an image upon background activation that pushes on top of the view and is displayed first in place of the snapshot when the user returns to the app, but I would like this feature to go away entirely and not have to save any snapshots or any other screen capture when the user goes into the app again. Can anyone suggest a way to do this?
Note that I am not talking about taking a screenshot with home button + sleep button press.
回答1:
In the applicationWillResignActive:
method of your app delegate, you can make a UIImageView of something inconspicuous pop-up. For example, let's say your application holds a user's passwords. Someone is walking behind the user and the user presses the home button. This calls the applicationWillResignActive:
method, which puts up perhaps a map view, so when the walker is gone and the user reopens the application, it opens directly to a map view instead of to a brief snapshot of the users passwords. This is how you might implement it:
//AppDelegate.m
- (void)applicationWillResignActive:(UIApplication *)application {
UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"picOfMap.png"]];
image.frame = self.view.frame;
image.contentMode = UIViewContentModeCenter;
[self.window.rootViewController.view addSubview: image];
}
And here's a description of when applicationWillResignActive:
is called:
This method is called to let your application know that it is about to move from the active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. An application in the inactive state continues to run but does not dispatch incoming events to responders. (UIApplicationDelegate Reference)
There may be some bugs in the code because I'm not familiar with working in the App Delegate, but that's the gist of what you should do.
回答2:
Here is a Technical Q&A by Apple on this topic: https://developer.apple.com/library/archive/qa/qa1838/_index.html
In short, code snippets in Swift are following:
func applicationDidEnterBackground(_ application: UIApplication) {
let someVCToShowInAppSwitcher = ...
window?.rootViewController?.present(someVCToShowInAppSwitcher, animated: false)
}
and
func applicationWillEnterForeground(_ application: UIApplication) {
// Make sure that we'll dismiss an expected VC, e.g.:
if window?.rootViewController?.presentedViewController?.view.accessibilityIdentifier == "someVCToShowInAppSwitcher" {
window?.rootViewController?.dismiss(sender: self)
}
}
来源:https://stackoverflow.com/questions/12607153/prevent-system-from-taking-a-use-on-resume-snapshot-of-app