亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

applicationWillEnterForeground與applicationDid

applicationWillEnterForeground與applicationDid

子衿沉夜 2019-12-07 14:29:20
當應用程序從后臺喚醒并且您希望它準備好處于活動狀態時,哪個合適的委托實現?applicationWillEnterForeground與applicationDidBecomeActive-有什么區別?當應用程序要進入睡眠狀態并且您想為其準備清理和保存數據時,哪個合適的委托實現?applicationWillResignActive與applicationDidEnterBackground-有什么區別?另外,我注意到當傳入的SMS或呼叫進入時,會調用applicationWillResignActive,但是用戶選擇單擊“確定”并繼續。我不希望我的應用在這些情況下采取任何措施。我只希望它繼續運行而無需任何中間清理,因為用戶沒有退出該應用程序。因此,我認為僅在applicationDidEnterBackground中進行清理工作更有意義。我希望您能就最佳做法提供寶貴的意見,然后再選擇要實施的代表來喚醒和入睡,以及考慮諸如SMS /呼叫中斷等事件。
查看完整描述

3 回答

?
縹緲止盈

TA貢獻2041條經驗 獲得超4個贊

喚醒時即重新啟動應用程序(通過跳板,應用程序切換或URL)applicationWillEnterForeground:。在應用程序準備就緒后,僅在后臺運行后才執行一次,而applicationDidBecomeActive:在啟動后可能會被多次調用。這applicationWillEnterForeground:對于重新啟動后只需進行一次設置的理想選擇。

applicationWillEnterForeground: 叫做:

  • 重新啟動應用程序時

  • 之前 applicationDidBecomeActive:

applicationDidBecomeActive: 叫做:

  • 應用首次啟動后的時間 application:didFinishLaunchingWithOptions:

  • applicationWillEnterForeground:如果沒有URL處理。

  • 之后application:handleOpenURL:被稱為。

  • 之后applicationWillResignActive:,如果用戶忽略中斷就像一個電話或短信。

applicationWillResignActive: 叫做:

  • 當有電話之類的干擾時。

    • 如果用戶接聽電話applicationDidEnterBackground:被呼叫。

    • 如果用戶忽略呼叫applicationDidBecomeActive:被調用。

  • 當按下主屏幕按鈕或用戶切換應用程序時。

  • 文檔說你應該

    • 暫停正在進行的任務

    • 禁用計時器

    • 暫停游戲

    • 降低OpenGL幀率

applicationDidEnterBackground: 叫做:

  • 后 applicationWillResignActive:

  • 文檔說您應該:

    • 釋放共享資源

    • 保存用戶數據

    • 使計時器無效

    • 保存應用程序狀態,以便在應用程序終止時可以將其恢復。

    • 禁用UI更新

  • 您有5秒的時間做您需要做的事情并返回方法

    • 如果您未在約5秒鐘內返回,則該應用程序將終止。

    • 您可以要求更多時間 beginBackgroundTaskWithExpirationHandler:


查看完整回答
反對 回復 2019-12-07
?
鴻蒙傳說

TA貢獻1865條經驗 獲得超7個贊

管理應用程序的生命周期有助于解決您的問題。有關快速概念,您可以參閱該文檔中的圖。您還可以從XCode向導生成的代碼中讀取注釋。列出如下:


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    // Override point for customization after application launch.

    return YES;

}


- (void)applicationWillResignActive:(UIApplication *)application

{

    /*

     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.

     */

}


- (void)applicationDidEnterBackground:(UIApplication *)application

{

    /*

     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.

     */

}


- (void)applicationWillEnterForeground:(UIApplication *)application

{

    /*

     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.

     */

}


- (void)applicationDidBecomeActive:(UIApplication *)application

{

    /*

     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.

     */

}


- (void)applicationWillTerminate:(UIApplication *)application

{

    /*

     Called when the application is about to terminate.

     Save data if appropriate.

     See also applicationDidEnterBackground:.

     */

}

有關更多詳細說明,請參閱UIApplicationDelegate的官方文檔。


查看完整回答
反對 回復 2019-12-07
?
絕地無雙

TA貢獻1946條經驗 獲得超4個贊

我仍然對Dano的回答有些困惑,因此我做了一些測試以獲取某些情況下的事件流以供參考,但是它對您也可能有用。這適用UIApplicationExitsOnSuspend于不在info.plist中使用的應用。這是在iOS 8模擬器上進行的,并已通過iOS 7設備確認。請原諒Xamarin的事件處理程序名稱。它們非常相似。


從非運行狀態進行的初始啟動和所有后續啟動:

完成啟動


激活


中斷(電話,頂部向下滑動,底部向上滑動):

主頁按鈕雙擊列出不活動的應用程序,然后重新選擇我們的應用程序:

OnResignActivation


激活


主頁按鈕連按兩次列出不活動的應用程序,選擇另一個應用程序,然后重新啟動我們的應用程序:

按下主頁按鈕,然后重新啟動:

鎖定(開/關按鈕),然后解鎖:

OnResignActivation


DidEnterBackground


進入前景


激活


雙擊主屏幕按鈕,然后終止我們的應用程序:(隨后的重新啟動是第一種情況)

OnResignActivation


DidEnterBackground


DidEnterBackground(僅限iOS 7?)


是的,DidEnterBackground在iOS7設備上被兩次調用。兩次UIApplication狀態均為Background。但是,iOS 8模擬器沒有。這需要在iO


查看完整回答
反對 回復 2019-12-07
  • 3 回答
  • 0 關注
  • 1780 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號