当应用被后台Kill掉的时候希望从后台返回的时候显示进入后台之前的内容

在Appdelegate中设置

- (BOOL)application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder{
return YES;
}
- (BOOL)application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder{
return YES;
}

为每个ViewController设置:restorationIdentifier(可以直接在sb中设置),restorationClass

如果设置了restorationClas则必须遵守UIViewControllerRestoration协议返回恢复时的控制器

+ (UIViewController *)viewControllerWithRestorationIdentifierPath:(NSArray<NSString *> *)identifierComponents coder:(NSCoder *)coder{
UIStoryboard *storybord = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
UIViewController *controller = [storybord instantiateViewControllerWithIdentifier:@"mywebcontroller"];
controller.restorationIdentifier = [identifierComponents lastObject];
controller.restorationClass = [MyWebViewController class];
return controller;
}

如果未指定代理方法则从Appdelgate中的代理中返回

If a view controller has an associated restoration class, the viewControllerWithRestorationIdentifierPath:coder: method of that class is called during state restoration. That method is responsible for returning the view controller object that matches the indicated view controller. If you do not specify a restoration class for your view controller, the state restoration engine asks your app delegate to provide the view controller object instead.

在控制器中保存和恢复状态

//只保存状态,如果是数据的话保存在文件中如NSUserdefault等
- (void)encodeRestorableStateWithCoder:(NSCoder *)coder{
//保存开关状态
[coder encodeBool:_switchBtn.isOn forKey:@"ison"]; [super encodeRestorableStateWithCoder:coder];
}
- (void)decodeRestorableStateWithCoder:(NSCoder *)coder{
[super decodeRestorableStateWithCoder:coder];
//获取开关状态
_switchBtn.on = [coder decodeBoolForKey:@"ison"]; }

iOS应用状态保存和恢复的更多相关文章

  1. Android Fragment使用(三) Activity, Fragment, WebView的状态保存和恢复

    Android中的状态保存和恢复 Android中的状态保存和恢复, 包括Activity和Fragment以及其中View的状态处理. Activity的状态除了其中的View和Fragment的状 ...

  2. Android学习总结——Activity状态保存和恢复

    Android中启动一个Activity如果点击Home键该Activity是不会被销毁的,但是当进行某些操作时某些数据就会丢失,如下: Java class: package com.king.ac ...

  3. vim状态保存跟恢复

    当我们结束了一天的工作的时候,可能手头的工作仅仅进行了一半,比如我们正在用vim修改一个android 问题,我们定位了问题关键,牵扯到了好几个类,如果这时候我们直接把vim关闭了,那我们下次还要重新 ...

  4. delphi IOS 后台状态保存

    FormSaveState procedure TFrm.FormSaveState(Sender: TObject);begin end; http://stackoverflow.com/ques ...

  5. 【Android】11.2 通过重写对应的方法保存和恢复实例的状态

    分类:C#.Android.VS2015: 创建日期:2016-02-21 一.简介 通过重写(也叫回调)对应的方法来管理Activity的生命周期,比如用户旋转屏幕时应用程序要能自动保存和恢复实例的 ...

  6. 如何禁止App在后台运行以及如何保存和恢复App的状态

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 如果禁止App在后台运行 iOS上的App类似于Windows ...

  7. Android之Activity状态的保存和恢复

    系统在某些情况下会调用onSaveInstanceState()和onRestoreInstanceState() 这两个方法来保存和恢复Activity的状态. 一句话:Activity在" ...

  8. Android课程---Activity中保存和恢复用户状态

    onSaveInstanceState 保存 在暂停之后和保存之前调用 onRestoreInstanceState 恢复 再启动之后和显示之前调用 package com.example.chens ...

  9. ios开发——实用技术篇&数据保存于恢复

    数据保存于恢复 用户操作(输入数据)之后,应用程序退出并且终止之后,当用户再次打开应用的时候还是保持原来的状态 一:在storyBoard中设置恢复标志符 二:在AppDalegate中代理方法 -( ...

随机推荐

  1. 为什么printf()用%f输出double型,而scanf却用%lf呢?

    转:https://blog.csdn.net/bat67/article/details/52056057 示例:double x:scanf(“%f”,&x):输入“123.4”,输出x的 ...

  2. 善用mysql中的FROM_UNIXTIME()函数和UNIX_TIMESTAMP()函数

    我们经常会面临要从数据库里判断时间,取出特定日期的查询.但是数据库里储存的都是unix时间戳,处理起来并不是特别友好.幸而MYSQL提供了几个处理时间戳的函数,可以帮助我们在查询的时候,就将时间戳格式 ...

  3. 【IoT】物联网NB-IoT之电信物联网开放平台对接流程浅析

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/liwei16611/article/de ...

  4. [LeetCode] 218. The Skyline Problem 天际线问题

    A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...

  5. [LeetCode] 637. Average of Levels in Binary Tree 二叉树的层平均值

    Given a non-empty binary tree, return the average value of the nodes on each level in the form of an ...

  6. ajax中参数traditional的作用

    在使用ajax向后台传值的时候,有的时 $.ajax({ type: "post", async: true, data: { "records": [&quo ...

  7. 10分钟弄懂Raft算法

    分布式系统在极大提高可用性.容错性的同时,带来了一致性问题(CAP理论).Raft算法能够解决分布式系统环境下的一致性问题. 我们熟悉的ETCD注册中心就采用了这个算法:你现在看的这篇微信公众号文章, ...

  8. Django文档阅读之执行原始SQL查询

    Django提供了两种执行原始SQL查询的方法:可以使用Manager.raw()来执行原始查询并返回模型实例,或者可以完全避免模型层直接执行自定义SQL. 每次编写原始SQL时都要关注防止SQL注入 ...

  9. VyOS 关于dhcp server 和dhcp relay 切换需要注意的

    dhcp server : /config/dhcpd.leases dhcp relay 两个dhcp 模式切换需要删除这个配置文件

  10. 采用socket传输文件

    采用socket传输文件 客户端输入文件的地址,服务端判断文件存在,就将文件传输到客户端 package com.fly.socket; import java.io.BufferedInputStr ...