Swift Save Viewcontroller State Using Coder Beyond App Close

Swift Save viewcontroller state using coder beyond app close

TL;DR:
There are key steps and methods required to implement state restoration in your app.

  1. Opting-in for App State Restoration in App Delegate by returning true for these methods shouldSaveApplicationState and shouldRestoreApplicationState.
  2. Setting Restoration Identifier for view controllers that you want state restoration implemented in.
  3. Implementing encodeRestorableStateWithCoder and decodeRestorableStateWithCoder methods in your view controller. The former is used to save any state information of your view controller to disk using encodeObjectForKey method. The latter shall be used to restore the state back from your saved contents to the disk by using decodeObjectForKey method.

Watch this awesome blog post about State Restoration for easier grasp. If you have time, also do spend on watching State Restoration WWDC Session.


There is no need to save your entire ViewController yourself. UIKit does that for you when you set your Restoration Identifier (In Interface Builder). The only thing we need to focus for state restoration is to save your essential properties needed to re-create your app's "State", for example, a Bool property which determines whether you want to display a specific button or not.

Now coming to your series of questions....

Can I just save self.view and can it save all subviews automatically ?

You do not need to save any of your views. All subviews will be handled by UIKit. Encode your required properties (such as Bool flags, count variables, key properties that can be used to fetch data from api call for your datasource etc.) inside encodeRestorableStateWithCoder method. Don't forget to re-contruct your view controller's state from decodeRestorableStateWithCoder. Both of these methods belond to UIStateRestoring protocol.

I need to init without coder at start by just using

No need to do any fancy inits.

How do I save all this coded data to file using NSKeyedUnarchiver and
retrieve back?

As I said earlier, implement necessary UIStateRestoring protocol methods to save and restore your app's state.

Swift Store and Restore ViewController state on Button Click

But I need to store and restore the state of the view controller on
button click with passing identified key.

Nope. This cannot be done on a button click. UIKit provides the coder to encode your data only when they the app is entering BG mode. There is most possibly no way to manually call those methods. State Preservation/Restoration is not designed for that, or at least UIKit does not allow this for now. You may have to write your own State Restoring mechanism yourself with, for example UserDefaults or writing to disk.

Open in same view controller as closed on (swift)

Updated to Swift 5

You can use UserDefaults which will remember your last ViewController.

First of all, you have to store some integer when you load any view like shown below:

FirstView.swift

 override func viewDidLoad() {
super.viewDidLoad()
UserDefaults.standard.setValue(0, forKey: "View")
// Do any additional setup after loading the view, typically from a nib.
}

SecondViewController.swift

override func viewDidLoad() {
super.viewDidLoad()
UserDefaults.standard.setValue(1, forKey: "View")
// Do any additional setup after loading the view.
}

Now In your AppDelegate.swift read that stored values and load the ViewController related to that Integer this way:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

let viewCount = UserDefaults.standard.integer(forKey: "View")
var VC = UIViewController()
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
println(viewCount)
if viewCount == 0 {
//FirstView should initiate
VC = storyboard.instantiateViewController(withIdentifier: "First") as! ViewController
} else if viewCount == 1 {
//SecondView should inititate
VC = storyboard.instantiateViewController(withIdentifier: "Second") as! SecondViewController
} else {
//ThirdView Should Initiate
VC = storyboard.instantiateViewController(withIdentifier: "Third") as! ThirdViewController
}

self.window?.makeKeyAndVisible()
self.window?.rootViewController = VC

return true
}

Check out THIS sample project for more Info.



Related Topics



Leave a reply



Submit