Question

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.

Was it helpful?

Solution

Look for – applicationDidEnterBackground: instead of applicationWillTerminate.

In iOS 4.0 and later, this method is called instead of the applicationWillTerminate: method when the user quits an application that supports background execution.

applicationWillTerminate represents 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 applicationWillTerminate message when it is terminated, so applicationDidEnterBackground: is the last "guaranteed" chance you have to clean up after yourself when the user quits the app.

Anyway, you can still get applicationWillTerminate to be called when the user quits your app (and your app terminated accordingly) if you set UIApplicationExitsOnSuspend in your app plist file.

For the sake of completeness, I will also add that applicationWillTerminate:

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.

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:

  1. applicationDidEnterBackground if you are running iOS4+ and do not set UIApplicationExitsOnSuspend in your app plist file;

  2. applicationWillTerminate if you are running on iOS3 and set UIApplicationExitsOnSuspend in your app plist file.

OTHER TIPS

as you will not get an applicationWillTerminate: if you are running on iOS 4+ (and app does not quit forcefully on entering background) if you really looking for cleanup the stuff only if the application gets terminated, you should do it in applicaitonDidFinishedLaunching:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top