在 iOS/iPadOS 13 上手动保存场景 NSUserActivity

Manually saving scene NSUserActivity on iOS/iPadOS 13

我使用NSUserActivity实现了iOS/iPadOS13状态恢复,主要是因为我的应用支持多场景

问题是,当我 运行 具有两个并排场景的模拟器并通过重建活动强制重启应用程序时,未保存并且 UI 不正确重新启动时恢复。

另一方面,如果我先返回 iPad 主屏幕,然后强制重新启动应用程序,活动就会正常恢复。

这是否意味着我的应用程序崩溃会阻止保存这些活动,从而导致重新启动时 UI 状态不一致?

我怎样才能定期 "force" 保存活动(接到 stateRestorationActivity(for scene:) 电话)?

The problem is that when I'm running the Simulator with two side-by-side scenes and force restart the app by rebuilding the activities are not saved and the UI is not properly restored on relaunch.

您在“通过重建强制重启应用”时看到的是预期行为。当您像那样强制重启时,应用程序没有机会保存您的 NSUserActivities。根据 Apple 的 state restoration tutorial:

If you want to test your app’s ability to restore its state, do not use the app switcher to kill the the app during debugging. Instead, use Xcode to kill the app, or kill the app programmatically. One technique is to suspend your app using the Home button, and then stop the debugger in Xcode. When you launch the app again using Xcode, UIKit initiates the state restoration process.

我知道您并没有在切换器中关闭应用程序,但任何“强制”停止都会禁用状态恢复(带有 警告)。要测试状态恢复,请改用上面 Apple 教程建议的技术。

Would this mean that a crash of my app would prevent saving these activities leading to an inconsistent UI state on relaunch?

我也这么认为。这是之前的 警告。虽然 Apple 表示终止应用程序(无论是在应用程序切换器中还是由于崩溃或其他原因)将禁用状态恢复,但这不一定是真的。您可以亲眼看到 Apple 自己的应用程序在应用程序切换器中被杀死并重新打开时存在奇怪的状态恢复不一致(您可能需要尝试几次才能获得不一致的行为)。虽然我还没有对此进行测试,但我想这些相同的不一致可能会因应用程序崩溃而发生。