2016-11-30 73 views
10

有時用戶按下主頁按鈕並關閉最近列表中的應用程序。檢測我的應用程序的意外關閉

我想通過諸如「This application not properly closed last time」之類的消息來警告用戶。

如何檢測這種意外關閉應用程序?有什麼辦法可以做到嗎? 另外我想保存用戶填寫的數據。

下面的方法工作良好,但在留在後臺一段時間後關閉應用程序將不會被調用到此方法。

- (void)applicationWillTerminate:(UIApplication *)application 

swift 3.0有沒有解決方案?

+8

沒有意外關閉應用程序這樣的事情。在後臺應用程序被殺害是正常的。你的應用應該正確處理這個。向用戶顯示這樣的消息是沒有意義的。 – rmaddy

+1

'我想告訴用戶一個消息,比如「這個應用程序沒有上次正確關閉」。 - 這是違反Apple的做法......使用最近的列表**關閉應用程序是**正確的方式關閉iOS上的應用程序 –

+0

@MihaiFratu是的..我同意你的看法,但也有關閉應用程序的另一種可能性..我們如何管理該應用程序 – Saranjith

回答

-3

在應用程序委託中,您可以檢查不同的事件。檢查applicationWillResignActive方法或ApplicationDidEnterBackground。

適當的位置將取決於您的用例。

+0

我同意你的意見。但我怎麼知道應用程序已關閉或不關閉 – Saranjith

+0

通常,當用戶按下主頁鍵時,最前面的應用程序將移至背景,然後移至暫停狀態。它不會馬上關閉。我的猜測是,您每次應用程序轉到後臺時都試圖保存信息。在這種情況下,您可以在應用程序中添加您的保存邏輯WillResignActive。此外,爲了下一次提醒用戶,您可以將變量保存在NSUserDefaults中,檢查該值並在需要時提醒用戶。 – TheAppMentor

-1

此方法可讓您的應用程序知道它即將被終止並從內存中完全清除。您應該使用此方法爲您的應用程序執行任何最終清理任務,例如釋放共享資源,保存用戶數據以及使定時器無效。您執行此方法大約需要五秒鐘來執行任何任務並返回。如果該方法在時間到期之前沒有返回,系統可能會完全終止該過程。

-(void) applicationWillTerminate:(UIApplication *)application{ } 
+0

它運行良好,但在後臺停留更多時間並從最近的菜單中關閉後不會調用此方法 – Saranjith

-2

可以使用的AppDelegate方法resignActive保存數據時,用戶發送應用背景。如果從didFinishLaunching方法啓動並在方法didBecomeActive中檢查此布爾值和保存的數據以進行計算,則在NSUserDefaults中保存一個布爾值true。使用數據和布爾值可以顯示警報並重置這兩個值。

4

要保存用戶填寫的數據,應該使用func applicationDidEnterBackground(_ application: UIApplication)函數。

這是功能描述:

//使用這個方法來釋放共享資源,保存用戶數據, 無效定時器和足夠的應用程序狀態信息存儲到 您的應用程序恢復到其當前狀態它會在稍後終止 。 //如果您的應用程序支持後臺執行,則調用此方法而不是applicationWillTerminate:當用戶 退出時。

至於「上次沒有正確關閉此應用程序」消息,您想要顯示用戶 - 顯示此類消息是錯誤的。 關閉應用程序的方法是按主屏幕並從列表中關閉它,所以這是預期的行爲。

+4

這應該被接受,因爲它是正確的答案。另外,它表明*你不應該做這樣的對話*。基本上,iOS上關於應用程序關閉的原因是「這不關你的事」。無論是用戶的結果(從任務管理器刷卡)還是系統,都需要「準備好」終止。儘管整個生命週期比經常更復雜。請參閱https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/TheAppLifeCycle/TheAppLifeCycle.html#//apple_ref/doc/uid/TP40007072-CH2-SW3 – Gero

+1

哦,一個小的補充:Don不要只依賴項目模板的應用程序委託中的評論。那些可能會過時,或者至少會過分簡化事情。我鏈接的完整文檔以及正確的方法文檔更清楚地解釋了這一點。特別是'applicationWillTerminate'方法中的註釋有點令人誤解(從我們在iOS上進行後臺執行和多任務處理之前的日子來看,它基本上是一樣的)。 – Gero

0

請檢查該瞭解的iOS應用程序生命週期是如何工作 - https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/TheAppLifeCycle/TheAppLifeCycle.html

現在,在你的項目中發現這下面的方法來跟蹤應用程序的生命週期過程。

對於目標C

- (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)applicationWillTerminate:(UIApplication *)application{ 
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 
    // Saves changes in the application's managed object context before the application terminates. 
} 

爲SWIFT(3.0)

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. 
} 

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. 
} 

func applicationWillTerminate(_ application: UIApplication) { 
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 
    // Saves changes in the application's managed object context before the application terminates. 
} 

希望這有助於。

-1

如果用戶在應用程序未掛起時手動殺死應用程序,將在您的應用程序委託中調用applicationWillTerminate

當用戶只需按主頁按鈕,applicationDidEnterBackground在您的應用程序委託中被調用。

如果applicationDidEnterBackground被調用,而applicationWillTerminate可能應用程序沒有正確殺害。

我說可能是因爲沒有保證,applicationWillTerminate被稱爲應用程序殺死的情況下。事實上,如果應用程序被暫停,該方法將不會被調用。

對於支持後臺執行的應用程序,這種方法通常是當用戶退出該應用,因爲該應用簡單地移動到 在這種情況下,背景 不被調用。但是,可能會在 應用程序在後臺運行(未暫停) 且系統因某種原因需要終止該應用程序的情況下調用此方法。

來源:https://developer.apple.com/reference/uikit/uiapplicationdelegate/1623111-applicationwillterminate

時手動殺不會造成這種方法被稱爲是當用戶按下home鍵,並在一段時間後,他按兩次home鍵,並終止該應用場景。在這種情況下,當用戶按下主頁按鈕時,iOS會在您的委託中調用applicationDidEnterBackground,並且在約5秒後,應用程序將獲得掛起狀態。當用戶稍後殺死應用時,其狀態將被暫停,並且willTerminate方法將不會被調用。

同樣的情況發生時,如果暫停iOS殺死應用程序以獲取資源給予終止狀態。

我會做的是堅持applicationDidEnterBackground方法調用的時間戳,取消它在application​Will​Enter​ForegroundapplicationWillTerminate

如果下一次你application(_:​will​Finish​Launching​With​Options:​)被調用你有保存在applicationDidEnterBackground時間戳值則意味着用戶沒有手動終止應用程序(可能),他把它在後臺後沒有回來。

如果用戶在應用程序被暫停期間殺了應用程序,也許在您的情況下,它仍然會被視爲濫用應用程序,並且可以顯示該消息,以便涵蓋所有用例。

+0

這將是很好的評論反對票。 –

-1

你不應該這樣做,但如果我們談論理論,那麼解決方案是顯示警告總是,除了第一次運行。

我會在UserDefaults中保存一個布爾值,我們稱之爲shouldShowWarning

事都可以往裏走:

UIApplicationDelegate.application(_:​did​Finish​Launching​With​Options:​) 

裏面的方法,你可以檢查shouldShowWarning是否true。如果是true,請顯示警告。 然後將值設置爲true

現在,如果應用程序終止並返回,則會顯示警報。

如果在某些情況下當您從代碼(非常標準)終止應用程序時,只需將shouldShowWarning設置爲false即可在應用程序重新啓動時禁用警告。