I'm trying to restore ViewController created from code. I'm trying to make very simple example but I don't understand why it doesn't work. An application changes background color of view by clicking on image and should save it when app is stopped. I'm trying to store and restore Bool property, and basing on it app changes views background color. When I'm testing it I'm sending app to background by cmd + shift + h, after that stop app from xcode and open it again, but it doesn't save selected background color.
- I've enabled restoration option in AppDelegate
- In ViewController I've set restorationIdentifier and restorationClass
- Adopted protocol UIViewControllerRestoration in extension to ViewController and implemented encoding and decoding methods
- Implemented ViewController restoring method
AppDelegate class
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = ViewController()
window?.makeKeyAndVisible()
return true
}
func application(_ application: UIApplication, shouldSaveApplicationState coder: NSCoder) -> Bool {
return true
}
func application(_ application: UIApplication, shouldRestoreApplicationState coder: NSCoder) -> Bool {
return true
}
}
ViewController class
class ViewController: UIViewController {
var sunny: Bool = true {
didSet {
setColor()
}
}
lazy var colorView: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
lazy var changeColor: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Change", for: .normal)
button.addTarget(self, action: #selector(change), for: .touchUpInside)
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
self.restorationIdentifier = "myVC"
self.restorationClass = ViewController.self
view.backgroundColor = .white
setupUI()
}
@objc func change() {
sunny.toggle()
}
func setColor() {
colorView.backgroundColor = sunny == true ? .yellow : .darkGray
}
}
extension ViewController: UIViewControllerRestoration {
func setupUI() {
view.addSubview(colorView)
NSLayoutConstraint.activate([
colorView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
colorView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
colorView.widthAnchor.constraint(equalToConstant: view.frame.width),
colorView.heightAnchor.constraint(equalToConstant: 500)
])
view.addSubview(changeColor)
NSLayoutConstraint.activate([
changeColor.topAnchor.constraint(equalTo: colorView.bottomAnchor, constant: 10),
changeColor.centerXAnchor.constraint(equalTo: colorView.centerXAnchor),
changeColor.widthAnchor.constraint(equalToConstant: 100),
changeColor.heightAnchor.constraint(equalToConstant: 45)
])
}
override func encodeRestorableState(with coder: NSCoder) {
coder.encode(sunny, forKey: "isSunny")
super.encodeRestorableState(with: coder)
}
override func decodeRestorableState(with coder: NSCoder) {
guard let isSunny = coder.decodeBool(forKey: "isSunny") as? Bool else {return}
sunny = isSunny
setColor()
super.decodeRestorableState(with: coder)
}
static func viewController(withRestorationIdentifierPath identifierComponents: [String], coder: NSCoder) -> UIViewController? {
let vc = ViewController()
return vc
}
}