简体   繁体   English

iOS中的状态恢复

[英]state restoration in iOS

I am storing my application screen, so that when the application gets opened it will show the screen that was stored. 我正在存储我的应用程序屏幕,以便在应用程序打开时它将显示已存储的屏幕。 The application is navigation based. 该应用程序是基于导航的。

I have assigned the Restoration Ids to my two view controllers in main story board. 我已将Restoration Ids分配给主故事板中的两个视图控制器。 The first controller is the root view controller of the navigation controller. 第一个控制器是导航控制器的根视图控制器。 I have also assigned Restoration Id to the navigation controller. 我还为导航控制器分配了Restoration Id。 Now the problem is when I run the application I am getting the following warning: 现在问题是当我运行应用程序时,我收到以下警告:

Unable to create restoration in progress marker file. 无法创建正在进行的恢复标记文件。

Not sure what else need to be done. 不确定还需要做什么。

I had the same warning before and fixed it by doing the followings. 之前我有同样的警告并通过以下方式修复它。

I was using storyboard. 我在使用故事板。 My storyboard only included a navigation view controller and a view controller (which was the root view controller of that navigation view controller). 我的故事板只包括导航视图控制器和视图控制器(该导航视图控制器的根视图控制器)。 In my case, it was caused by either of the two reasons: 在我的情况下,它是由两个原因之一引起的:

The application was not set up correctly for state preservation The followings need to be set: 未正确设置应用程序以进行状态保存需要设置以下内容:

  1. In app delegate, override application:willFinishLaunching. 在app delegate中,覆盖应用程序:willFinishLaunching。 One can simply return YES in that method. 可以在该方法中简单地返回YES。

  2. For every view controllers and views (including the navigation view controllers and tab view controllers), set a restore ID 对于每个视图控制器和视图(包括导航视图控制器和选项卡视图控制器),请设置还原ID

  3. In app delegate, opt-in by overriding shouldSaveApplicationState and shouldRestoreApplicationState 在app delegate中,通过覆盖shouldSaveApplicationStateshouldRestoreApplicationState选择加入

If this warning still occurred, you could check how you run your app in Xcode. 如果仍然出现此警告,您可以检查在Xcode中如何运行应用程序。 I ran my app in Xcode simulator and had to follow a specific sequence to trigger state preservation. 我在Xcode模拟器中运行我的应用程序,并且必须遵循特定的顺序来触发状态保存。

  • launch the app in Xcode simulator 在Xcode模拟器中启动应用程序
  • In Xcode simulator, click the " Home " button to put the app into the background. 在Xcode模拟器中,单击“ 主页 ”按钮将应用程序放入后台。 The encodeRestorableStateWithCoder method of the view controller should be called 应该调用视图控制器的encodeRestorableStateWithCoder方法
  • go back to Xcode, click the " stop " button to terminate the simulation 返回Xcode,单击“ 停止 ”按钮终止模拟
  • In Xcode simulator, double click the "Home" button and then remove the app from the app switcher and 在Xcode模拟器中,双击“主页”按钮,然后从应用程序切换器中删除应用程序
  • go back to Xcode, run the app again. 回到Xcode,再次运行应用程序。

That warning should not appear. 不应该出现这个警告。 When I used other sequences, I saw that warning appearing. 当我使用其他序列时,我看到出现了警告。 When I debugged my app on my device using Xcode, I followed the same sequence and did not see the warning. 当我使用Xcode在我的设备上调试我的应用程序时,我按照相同的顺序,没有看到警告。

I think that warning indicated that because Xcode could not terminate the app correctly, the state restoration file was not correctly saved onto the disk. 我认为该警告表明,由于Xcode无法正确终止应用程序,因此状态恢复文件未正确保存到磁盘上。

Try to give your View a restoration ID. 尝试为您的View提供恢复ID。 You can do so to open your xib or storyboard and assign the id to your View Controller. 您可以这样做以打开xib或故事板并将ID分配给View Controller。

在此输入图像描述

more details: http://useyourloaf.com/blog/2013/05/21/state-preservation-and-restoration.html 更多详情: http//useyourloaf.com/blog/2013/05/21/state-preservation-and-restoration.html

Simulator: 模拟器:

  • This happens for the first time on the simulator when the app encodes. 这在应用程序编码时首次出现在模拟器上。 (not sure why) (不知道为什么)
  • Subsequent times this warning wouldn't be shown. 随后的时间不会显示此警告。

Device: 设备:

Try running on the device. 尝试在设备上运行。

  1. Run on device 在设备上运行
  2. Go to home screen. 转到主屏幕。
  3. Stop running on Xcode 停止在Xcode上运行
  4. Remove app from app switcher 从应用切换器中删除应用
  5. Run on device again 再次在设备上运行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM