状态恢复期间显示的是应用程序屏幕快照,而不是launchScreen [英] App screen snapshot being shown instead of launchScreen during state restoration

查看:288
本文介绍了状态恢复期间显示的是应用程序屏幕快照,而不是launchScreen的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了这种情况,我是第一次将应用程序状态恢复API添加到支持iOS 9.3及更高版本的新应用程序中.状态恢复在功能上可以正常工作,但是我注意到由于状态恢复期间的延迟,它显示的是屏幕快照而不是LaunchScreen.xib内容.通过快照,我指的是iOS进入背景时iOS为您的应用程序UI拍摄的自动屏幕截图.

I encountered this situation where I had added app state restoration APIs for the first time to a new app supporting iOS 9.3 and higher. The state restoration is working fine functionally, but I noticed that it was showing a screen snapshot instead of the LaunchScreen.xib content because of the delay during state restoration. By snapshot I'm referring to the automatic screenshot iOS takes of your apps UI as it goes into the background.

如果您不知道什么是应用状态恢复,它会随iOS 6一起发布,这是Apple为其提供的链接:

If you don't know what app state restoration is, it came out with iOS 6, here's the link from Apple on it:

有关应用程序状态保存"的Apple文档恢复

在这种应用情况下显示屏幕快照是一个严重的问题,因为在运行iOS 9.3.5的iPhone 4s上,此特定应用有大约4秒钟的延迟,需要在应用状态恢复期间执行.由于这段时间未显示启动/启动屏幕,因此用户会认为该应用已被挂起.在模拟器和设备上,当前在所有可用的iOS版本上都会重现这种情况.

Showing the screen snapshot in this apps case is a significant problem as this particular app has about 4 seconds of delay, on an iPhone 4s running iOS 9.3.5, to do during app state restoration. Users would perceive the app to be hung since the launch / splash screen was not being shown during this time. This situation reproduces on all iOS versions currently available, on both simulator and device.

如何在仍保留应用程序状态保存/恢复功能好处的同时,阻止显示快照并强制始终使用LaunchScreen.xib?

How can I prevent the snapshot from being shown, and force the LaunchScreen.xib to be used always, all while still preserving the app state save/restore functionality benefits?

推荐答案

经过研究,我发现Apple很久以前就提供了UIApplication的方法来处理这种情况.但是,即使在今天,它的用法也很少有记载.

After researching this I found that Apple has long ago provided a method off of UIApplication to deal with this situation. But its usage, even today, is poorly documented.

解决方案是使用UIApplication中的ignoreSnapshotOnNextAppliationLaunch方法.

The solution is to use the ignoreSnapshotOnNextAppliationLaunch method from UIApplication.

Apple ignoreSnapshotOnNextApplicationLaunch方法

您将必须通过Apple在此处建议的UIApplication单例模式来访问它,我将对此进行解释:

You will have to access it via the UIApplication singleton pattern as suggested by Apple here as I will explain:

Apple UIApplication sharedApplication方法

使用 的地方没有明确记录,我在这里分享.除非在iOS正在从视图控制器保存应用程序状态时明确调用,否则ignoreSnapshotOnNextApplicationLaunch方法将完全无效.例如,当您点击主页"按钮以使应用程序后台运行时.

The where to use this is what is not clearly documented and that I am sharing here. The ignoreSnapshotOnNextApplicationLaunch method will have absolutely no effect unless specifically called when iOS is saving the app state from the view controller(s). Such as when you tap the home button to background the app.

您不能直接从处理背景/前景过渡的AppDelegate方法中调用此方法,因为需要在保存其状态以供以后还原时从视图控制器中调用它.

You cannot call this method directly from the AppDelegate methods dealing with background / foreground transitions, as it needs to be called from the view controllers while their states are being saved for later restoration.

对于此保存任务,Apple提供了UIViewController中的encodeRestorableStateWithCoder方法

For this saving task Apple provides the encodeRestorableStateWithCoder method from UIViewController

Apple encodeRestorableStateWithCoder方法

这是我们需要进行更改的地方.如果要进行状态还原,您应该已经拥有了.但是,通过将此方法调用添加到每个您在情节提要中设置了恢复ID或手动保存状态的视图控制器类中,可以通过从UIApplication单例中包含ignoreSnapshotOnNextApplicationLaunch来避免使用任何快照. 不会会阻止iOS拍摄快照,只是不会在重新启动应用程序状态时显示快照.

And this is where we need to make the change. If doing state restoration you should already have it; but by adding this method call to each view controller class where you have setup restoration IDs in storyboard, or are saving state manually, you can avoid any snapshots being used by including the ignoreSnapshotOnNextApplicationLaunch from the UIApplication singleton. This will not prevent iOS from taking the snapshot, just not showing it during app state restoration on re-launch.

// 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删除以前保存的快照文件.

Be sure you re-background the app after adding this during your testing, to have iOS delete the previously saved snapshot file.

这篇关于状态恢复期间显示的是应用程序屏幕快照,而不是launchScreen的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆