Save State App

Appdelegate:

func application(application: UIApplication, shouldSaveApplicationState coder: NSCoder) -> Bool {
return true
}

func application(application: UIApplication, shouldRestoreApplicationState coder: NSCoder) -> Bool {
flagsSaveSate = true
return true
}

Add: Thing.swift

//
// Thing.swift
// iOrder
//
// Created by mhtran on 8/3/16.
// Copyright © 2016 mhtran. All rights reserved.
//

import UIKit

class Thing: NSObject , UIStateRestoring {
var word = ""

var restorationParent: UIStateRestoring? // unused

var objectRestorationClass: AnyObject.Type? // unused

func encodeRestorableStateWithCoder(coder: NSCoder) {
print("thing encode")
coder.encodeObject(self.word, forKey:"word")
}

func decodeRestorableStateWithCoder(coder: NSCoder) {
print("thing decode")
self.word = coder.decodeObjectForKey("word") as! String
}

func applicationFinishedRestoringState() {
print("finished thing")
}
}

VC that want save.

var thing : Thing!
override func awakeFromNib() {
super.awakeFromNib()
self.thing = self.dynamicType.makeThing()
}
override func encodeRestorableStateWithCoder(coder: NSCoder) {
super.encodeRestorableStateWithCoder(coder)
coder.encodeObject(self.thing, forKey: "mything") // must show this object to the archiver
}
override func applicationFinishedRestoringState() {
print("finished view controller")
// self.thing.restorationParent = self
}

add extension:

extension HomeVC : UIObjectRestoration {

class func makeThing () -> Thing {
let thing = Thing()
UIApplication.registerObjectForStateRestoration(thing, restorationIdentifier: "thing")
// thing.objectRestorationClass = self
return thing
}

// unused, no actual restoration, just showing it can be done
class func objectWithRestorationIdentifierPath(ip: [String],
coder: NSCoder) -> UIStateRestoring? {
print(ip)
let thing = self.makeThing()
return thing
}

}