Why is the restoration is showing all my view controllers?

54 Views Asked by At

I'm working with an app that presents a series of view controllers. There is no navigation controller because when the user goes through one view, we want him to continue to the next. At some point, we need access to the camera. If the user denies access, we present an alert with a button that will take him directly to the settings of our app. When the user changes the privacy setting, as is known, the app is terminated. So, when the user relaunches the app, I want him to land exactly with the view controller that was last displayed. To this end, I made sure that all my view controllers have their restoration IDs set. I'm also returning true to the restoration delegation methods application(:shouldSaveApplicationState:) and application(:shouldRestoreApplicationState:). It is kinda working except that every view controller is briefly displayed before the last one appears on the screen.

What am I missing?

I've tried implementing application(_:viewControllerWithRestorationIdentifierPath:coder:). When I do, this method is repeatedly called initially with the restoration ID of the very first view controller in the hierarchy in the string restoration IDs array. The second time it's called, the restoration ID of the second view controller is added to the array. And it does that for every single view controller until the last one that was displayed. I tried to return the view controller matching the last restoration ID in the array but that didn't change anything. Each view controller are briefly displayed.

func application(_ application: UIApplication, viewControllerWithRestorationIdentifierPath identifierComponents: [String], coder: NSCoder) -> UIViewController? {
    let mainStoryboard = UIStoryboard.init(name: "Main", bundle: nil)
    let lastViewController = mainStoryboard.instantiateViewController(withIdentifier: identifierComponents.last!)
    return lastViewController
}

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

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

There are 0 best solutions below