当应用被后台Kill掉的时候但愿从后台返回的时候显示进入后台以前的内容web
- (BOOL)application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder{ return YES; } - (BOOL)application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder{ return YES; }
为每一个ViewController设置:restorationIdentifier(能够直接在sb中设置),restorationClassapp
若是设置了restorationClas则必须遵照UIViewControllerRestoration协议返回恢复时的控制器ide
+ (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中的代理中返回spa
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"]; }