经过研究,我发现 Apple 很久以前就提供了一种脱离 UIApplication 的方法来处理这种情况。但即使在今天,它的用法也没有得到很好的记录。
解决方案是使用 UIApplication 中的 ignoreSnapshotOnNextAppliationLaunch 方法。
Apple ignoreSnapshotOnNextApplicationLaunch method
您必须通过 Apple 建议的 UIApplication 单例模式访问它,我将在此处解释:
Apple UIApplication sharedApplication method
在哪里 使用它是没有明确记录的,我在这里分享。除非在 iOS 从视图控制器保存应用程序状态时特别调用,否则 ignoreSnapshotOnNextApplicationLaunch 方法将完全无效。例如,当您点击主页按钮以使应用程序后台运行时。
您不能直接从处理背景/前景转换的 AppDelegate 方法调用此方法,因为它需要在保存视图控制器状态以供以后恢复时从视图控制器调用。
对于这个保存任务,Apple 提供了 UIViewController 中的 encodeRestorableStateWithCoder 方法
Apple encodeRestorableStateWithCoder method
这就是我们需要做出改变的地方。如果进行状态恢复,您应该已经拥有它;但是通过将此方法调用添加到您在情节提要中设置恢复 ID 或手动保存状态的每个视图控制器类,您可以通过包含 UIApplication 单例中的 ignoreSnapshotOnNextApplicationLaunch 来避免使用任何快照。这将不会阻止 iOS 拍摄快照,只是在重新启动时恢复应用状态期间不显示它。
// save any app state information that is not already saved automatically
- (void)encodeRestorableStateWithCoder:(NSCoder *)coder {
// prevent taking a screen shapshot and force launchScreen xib to be used always
[[UIApplication sharedApplication] ignoreSnapshotOnNextApplicationLaunch];
[super encodeRestorableStateWithCoder:coder];
return;
}
确保在测试期间添加此应用后重新后台运行,以让 iOS 删除之前保存的快照文件。