首页 > 解决方案 > 如何让应用状态恢复正常工作?

问题描述

我有 2 个表视图控制器的简单应用程序,并且我试图在应用程序启动时保留第一个表视图控制器屏幕。当您从第二个表视图中选择一个单元格时,第一个表视图会动态填充。我一直在测试的方法是添加一个新单元格,最小化应用程序,从 Xcode 单击停止,然后按 Xcode 上的播放按钮。但是它没有做任何事情,我已经坚持了几天,我无法弄清楚我做错了什么,任何帮助将不胜感激。

我有我的 restoreId 设置:

 override func viewDidLoad() {
     super.viewDidLoad()

     restorationIdentifier = "MainTableViewController"        
}

这是我的 AppDelegate:

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        self.window?.makeKeyAndVisible()
        return true
    }

    // MARK: UISceneSession Lifecycle

    @available(iOS 13.0, *)
    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    @available(iOS 13.0, *)
    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }

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

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

    func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.makeKeyAndVisible()
        let viewController = MainTableViewController()
        let navC = UINavigationController(rootViewController: viewController)
        navC.restorationIdentifier = "MainTableViewController"
        window?.rootViewController = navC
        return true

    }

    func application(_ application: UIApplication, didDecodeRestorableStateWith coder: NSCoder) {
        UIApplication.shared.extendStateRestoration()
        DispatchQueue.main.async {
            UIApplication.shared.completeStateRestoration()
        }
    }
}


在我的第一个表视图控制器中,我有这个:

extension MainTableViewController: UIViewControllerRestoration {

    static let activityIdentifierKey = "MainActivity"

    override func encodeRestorableState(with coder: NSCoder) {
        super.encodeRestorableState(with: coder)
        coder.encode(exchangeRates, forKey: MainTableViewController.activityIdentifierKey)
    }

    override func decodeRestorableState(with coder: NSCoder) {
        super.decodeRestorableState(with: coder)
        if let returnedPersons = coder.decodeObject(forKey: MainTableViewController.activityIdentifierKey) as? [Person]{
            persons = returnedPersons
        }
    }

    override func applicationFinishedRestoringState() {
        tableView.reloadData()
    }

    static func viewController(withRestorationIdentifierPath identifierComponents: [String], coder: NSCoder) -> UIViewController? {
        guard let restoredPersons = coder.decodeObject(forKey: "MainActivity") as? [Persons] else {
            print("decoding User Detail")
            return nil
        }

        let vc = MainTableViewController()
        vc.persons = restoredPersons
        return vc
    }
}

我尝试在 iOS 13.2、iOS 12 和 iOS 11 上运行该应用程序

标签: iosswift

解决方案


使用此方法:

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

而不是弃用的方法:

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

推荐阅读