(iOS 4.x) Cleaning out the NSUserDefaults when app quits
So I have been doing a lot of reading about the way iOS handles application state notifications, and it looks like they may have created a problem for me in iOS 4.
Before 4.x, I was cleaning out my NSUserDefaults in the - (void)applicationWillTerminate:(UIApplication *)application {}
delegate method.
Unfortunately, it does not look like this method is used anymore when the user quits the app from the desktop (pressing the red "-"). Instead the app receives a SIGKIL. Has anyone schemed a way to capture this and do something when the app is terminated (such开发者_运维知识库 as clean out the UserDefaults)? I would prefer to not disable the multitasking since that's non-standard behavior. Any help at all would be appreciated. Thanks!
That method will still be sent if iOS ever decides to quit the app. You should probably put the same cleanup code in applicationWillResignActive: if you want it to clean up when moving to the background as well.
However, if you say you want to support multitasking, why are you wiping the default? Shouldn't the user be able to come back to the app exactly as they left it?
By "clean out" do you mean "synchronize to disk"? (i.e. by calling -synchronize)
If so, NSUserDefaults handles this for you automatically, no need to worry about it.
You still need the code in applicationWillTerminate: for those iOS 4 devices that don't support multi-tasking. But you'll also need to call the same code when the app goes into the background (applicationDidEnterBackground:). You might also consider applicationWillResignActive:, but this would get called when a call or an SMS arrives rather than only when it goes into the background.
精彩评论