How to perform last actions upon user exiting an iPhone app?

心已入冬 提交于 2019-12-11 06:21:59

问题


Is there a way to perform some last actions when the user kills the application on iPhone?

In UIApplicationDelegate there is applicationWillTerminate: but as I understand it's not guaranteed to get called when the application terminates. Is there another way?


回答1:


You can't rely on applicationWillTerminate being called. From the documentation:

For apps 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 app. For apps that support background execution, this method is generally not called when the user quits the app because the app simply moves to the background in that case. However, this method may be called in situations where the app is running in the background (not suspended) and the system needs to terminate it for some reason.

The proper place to save any state is when the app enters the background. Once that happens, there is no way to know if the app will return to the foreground or if it gets killed and then started from the beginning.




回答2:


All methods concerning your app state are in your AppDelegate when you use one of the project templates.

Put the code in the applicationWillResignActive: method. It will get called if your app goes to an inactive state (terminating or no).

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



回答3:


The "correct" place to save state is in both -applicationDidEnterBackground: and -applicationWillTerminate:. Don't worry about double-saving; generally only one of them is called (IME, -applicationWillTerminate: is not called when your app is killed in the background).

Caveat: -applicationDidEnterBackground: is not guaranteed to be called, since it is called after your app enters the background (and thus becomes eligible for killing without notice!). If the device is low on memory when your app is backgrounded, it might be killed. The best way to avoid this is to not use too much memory in the first place.

You could use -applicationWillResignActive:, but I do not recommend this: apps become inactive quite frequently. An obvious is system dialogs (location/privacy prompts, Wi-Fi, notifications that show as alerts, alarms), TWTweetSheet, and I suspect MFMailComposeViewController, MFMessageComposeViewController, Notification Center, the app-switcher bar (e.g. to change tracks/enable orientation lock).




回答4:


you can use applicationWillResignActive method in the appdelegate, or you can do the following, if you want to save stuff, but for some reason, you dont want to do it in the app delegate:

- (void) viewDidLoad/init { 
    [[NSNotificationCenter defaultCenter]
        addObserver:self
        selector:@selector(myApplicationWillResign)
        name:UIApplicationWillResignActiveNotification 
        object:NULL];
}

- (void) myApplicationWillResign {
    NSLog(@"About to die, perform last actions");
}


来源:https://stackoverflow.com/questions/15260410/how-to-perform-last-actions-upon-user-exiting-an-iphone-app

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!