My iPhone app will download several content to Documents folder while running the application. I want to delete the downloaded files when app is going to terminate. My app runs on target 4.3 or more and ARC enabled. What is the best place to delete content from documents folder ?
- (void)applicationWillTerminate:(UIApplication *)application
is not being called when the app is terminated.
Look for – applicationDidEnterBackground: instead of
applicationWillTerminate.applicationWillTerminaterepresents what “quitting” an app used to mean in pre-iOS4 world.In modern (i.e., iOS4+) apps, when the user quits the app, the app goes to background and is suspended (so the user can reenter it quickly). So, the app is not actually terminated, still iOS can terminate it anytime if it needs to recover memory. This happens quite often, meaning that if you suspend your app and go back to it the day after, you will find that the app was terminated. The bad thing is that a suspended app will not receive a
applicationWillTerminatemessage when it is terminated, soapplicationDidEnterBackground:is the last “guaranteed” chance you have to clean up after yourself when the user quits the app.Anyway, you can still get
applicationWillTerminateto be called when the user quits your app (and your app terminated accordingly) if you setUIApplicationExitsOnSuspendin your app plist file.For the sake of completeness, I will also add that
applicationWillTerminate:but this is a specific use case for an app which is actually running in the background (which is the case for a counted number of app types out there).
Summing it all up, the correct place to clean up the docs is:
applicationDidEnterBackgroundif you are running iOS4+ and do not setUIApplicationExitsOnSuspendin your app plist file;applicationWillTerminateif you are running on iOS3 and setUIApplicationExitsOnSuspendin your app plist file.