簡體   English   中英

什么時候應該存儲並重新存儲到ios swift上的鑰匙串?

[英]when should i store and re-store to keychain on ios swift?

我在appDelegate中看到了幾個方法,我不確定是否在其中一些方案中存儲和重新存儲用戶狀態涵蓋所有方案?

func applicationWillResignActive(application: UIApplication) {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

    stopTasks()
    setSharedPrefrences()
}

func applicationDidEnterBackground(application: UIApplication) {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

    stopTasks()
    setSharedPrefrences()
}

func applicationWillEnterForeground(application: UIApplication) {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.

    startTasks()
    getSharedPrefrences()
}

func applicationDidBecomeActive(application: UIApplication) {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    startTasks()
    getSharedPrefrences()
    connectGcmService(application)

}

func applicationWillTerminate(application: UIApplication) {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    stopTasks()
    setSharedPrefrences()
    disconnectGcmService(application)
}

我應該只在其中一些存儲\\還原嗎? 我應該何時斷開連接並重新連接到GCM服務?

我的恢復是多余的?

保持一個本地標志說是恢復已經做了不實用,因為應用程序可能會破壞它?

在Apple的iOS應用程序編程指南中了解應用程序生命周期:應用程序的執行狀態

此外, UIApplicationDelegate針對您在問題中提到的方法的文檔在調用時包含非常詳細的信息。 applicationWillResignActive的文檔為例。

  • didEnterBackground總是以willResignActive didEnterBackground ,因此不需要運行相同的代碼。

  • willEnterForeground總是后跟didBecomeActive ,但在其他情況下也可以調用didBecomeActive (見下文)。

  • 可以在沒有調用didEnterBackground情況下調用willResignActive ,例如10%的電池警告或電話呼叫。 如果用戶拒絕該呼叫,您的應用將保持在前台,並且接下來會調用didBecomeActive以告訴您該應用再次處於活動狀態。

  • willTerminate永遠不會在現代iOS應用程序中調用。 在iOS支持多任務處理之前,它在iOS 2和3中使用。 由於應用程序現在在用戶“退出”時移至后台,因此不再使用此事件。 (當操作系統因后台處於內存壓力而導致你的應用程序被殺死時,它會立即被殺死而不再執行任何代碼。)

綜上所述:

  • stopTasks / startTasks應該在willResignActivedidBecomeActive
  • 保存/恢復用戶數據可以在willResignActive / didBecomeActivedidEnterBackground / willEnterForeground 我可能會選擇后者。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM