- (void)applicationWillTerminate:(UIApplication *)application;为什么不会被调用。

来源:互联网 发布:淘宝怎么在百度推广 编辑:程序博客网 时间:2024/05/16 07:19

- (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里面去做。

0 0