- (void)applicationWillTerminate:(UIApplication *)application;爲什麼不會被調用。

- (void)applicationWillTerminate:(UIApplication *)application;在裏面移除NSUserDefaults的key爲什麼沒用呢?就是程序退出之後~

其實這是因爲- (void)applicationWillTerminate:(UIApplication*)application這個方法根本就沒有被調用。那麼這是爲什麼呢?翻開蘋果文檔我們將看到。

This method lets your application know that it is about to be terminated and purged from memory entirely. You should use this method to perform any final clean-up tasks for your application, such as freeing shared resources, saving user data, and invalidating timers. Your implementation of this method has approximately five seconds to perform any tasks and return. If the method does not return before time expires, the system may kill the process altogether.

For applications that do not support background execution or are linked against iOS 3.x or earlier, this method is always called when the user quits the application. For applications that support background execution, this method is generally not called when the user quits the application because the application simply moves to the background in that case. However, this method may be called in situations where the application is running in the background (not suspended) and the system needs to terminate it for some reason.

查字典之後,我們就可以知道蘋果說了這麼一回事:

這個方法讓你的應用知道它馬上要被幹掉了並且將從內存完全清除掉。你應該使用這個方法來執行任何你的應用最後的清理工作,比如釋放共享資源,保存用戶數據,使timers失效,實現這個方法你大約有5秒鐘的時間來執行你的任務並且返回,如果這個方法在時間失效前沒有返回,那麼系統可能會一併殺死這個進程。

對那些不支持後臺執行或者在ios3.x以及之前的應用,當用戶關閉應用這個方法總是會被調用。對支持後臺執行的應用,在用戶關閉應用的時候它一般不會被調用,因爲應用在那種情況下只是簡單的移動到了後臺。那麼這個方法什麼時候會被調用呢?當應用還在後臺運行(不是暫停在後臺),但是系統因爲某些原因需要關閉它的時候這個方法可能會被調用。

-----------------------------------------------------

下面我們來模擬這個方法會被調用的情況:

拷貝這段代碼到你的工程的APPDelegate裏去:

- (void)applicationDidEnterBackground:(UIApplication *)application

{

    NSLog(@"%s", __PRETTY_FUNCTION__);

    __block UIBackgroundTaskIdentifier identifier = [[UIApplication sharedApplication]beginBackgroundTaskWithExpirationHandler:^{

        if (identifier != UIBackgroundTaskInvalid) {

            [[UIApplication sharedApplication] endBackgroundTask:identifier];

            identifier = UIBackgroundTaskInvalid;

        }

    }];

    

    dispatch_async(dispatch_get_main_queue(), ^{

        for (int i=0; i < 20; i++) {

            NSLog(@"%d", i);

            sleep(1);

        }

        if (identifier != UIBackgroundTaskInvalid) {

            [[UIApplication sharedApplication] endBackgroundTask:identifier];

            identifier = UIBackgroundTaskInvalid;

        }

    });

}


- (void)applicationWillTerminate:(UIApplication *)application

{

    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"testKey"];

    [[NSUserDefaults standardUserDefaults] synchronize];

    NSLog(@"%s", __PRETTY_FUNCTION__);

 

}

我們將在控制檯看到激動人心的一幕


然後你的NSUserDefaults 也被刪除了。

-----------------------------------------------------

說了怎麼多之後,我們可以看到以前在

- (void)applicationWillTerminate:(UIApplication *)application這個方法中做的事情,我們現在應該移動到

- (void)applicationDidEnterBackground:(UIApplication *)application裏面去做。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章