[英]implementing state restoration in iOS 14
我正在嘗試在我的應用程序中實現狀態恢復,我已經閱讀了大量文章和文檔,但到目前為止我還沒有讓它工作。 我已經為所有視圖控制器提供了一個恢復 ID,並且(我認為)擁有讓它工作的所有必要功能。
在AppDelegate
func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
return true
}
func application(_ application: UIApplication, shouldRestoreSecureApplicationState coder: NSCoder) -> Bool {
return true
}
func application(_ application: UIApplication, shouldSaveSecureApplicationState coder: NSCoder) -> Bool {
return true
}
func application(_ application: UIApplication, viewControllerWithRestorationIdentifierPath identifierComponents: [String], coder: NSCoder) -> UIViewController? {
return coder.decodeObject(forKey: "Restoration ID") as? UIViewController
}
func application(_ application: UIApplication, didDecodeRestorableStateWith coder: NSCoder) {
UIApplication.shared.extendStateRestoration()
DispatchQueue.main.async {
UIApplication.shared.completeStateRestoration()
}
}
在我的視圖控制器中:
override func encodeRestorableState(with coder: NSCoder) {
super.encodeRestorableState(with: coder)
getSetDataFromTable()
coder.encode(currentSession, forKey: "CurrentSession")
}
override func decodeRestorableState(with coder: NSCoder) {
super.decodeRestorableState(with: coder)
self.currentSession = coder.decodeObject(forKey: "CurrentSession") as! [CurrentSessionElement]
}
override func applicationFinishedRestoringState() {
tableView.reloadData()
}
static func viewController(withRestorationIdentifierPath identifierComponents: [String], coder: NSCoder) -> UIViewController? {
guard let restoredSession = coder.decodeObject(forKey: "CurrentSession") as? [CurrentSessionElement] else {
print("decoding user detail")
return nil
}
let vc = CurrentSessionVC()
vc.currentSession = restoredSession
return vc
}
我在所有函數中設置了斷點,當我嘗試測試功能時,命中的斷點是
加載時: shouldRestoreSecureApplicationState
didDecodeRestorableStateWith
清除應用程序時: shouldSaveSecureApplicationState
測試時沒有任何恢復,並且沒有觸發任何視圖控制器功能,我錯過了什么嗎?
我遇到過同樣的問題。 我刪除了 SceneDelegate 並且一切都開始正常工作。 據我了解,SwiftUI 的恢復以另一種方式工作,與經典 UI 流程不兼容
看看這個。 不建議遠離 SceneDelegate。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.