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:

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的官方文檔。

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
- 3 回答
- 0 關注
- 1780 瀏覽
添加回答
舉報