views:

1269

answers:

3

I just got the iPhone SDK 4 and I'm trying to leave off developing an app I was working on before. So I implemented both -(void)applicationDidEnterBackground:(UIApplication *)application and - (void)applicationWillTerminate:(UIApplication *)application

When I click the home button in the simulator 4.0, it seems to work okay. The app calls -(void)applicationDidEnterBackground:(UIApplication *)application and enters the background state..

However, I then double click the home button to expose the app running in the background, and hold it down to show the minus sign in or to exit. When I click the minus sign, the debugger says

Program received signal: "SIGKILL"

in addition, - (void)applicationWillTerminate:(UIApplication *)application is never called. Am I missing something I need to do for this to work on OS4? Or at least can somebody point me to a way to debug SIGKILL. Thanks.

+2  A: 

-applicationWillTerminate: is only called if your application is in a running state when it is asked to quit. If it is already in a suspended state (as you have it here), the system simply sends a kill signal to the application. This happens when the user presses the red minus buttons to terminate the app manually as well as when the system decides to jettison the app to free up memory for the foreground application.

If you application is running in the background (playing music or running some background tasks, for instance) and the user manually terminates the application by pressing the minus button, only then will you actually get an -applicationWillTerminate: message. Therefore, you must save all your state/etc. before you return from -applicationDidEnterBackground:.

Jason Coco
Thanks for the reply. That make sense. However, I'm having another issue as well. When -applicationDidEnterBackground: is called, I try to save the state of the app using NSUserDefaults, but for some reason my values are not what I'm expecting them to be once the app restarts. Any reasons why this wouldn't be working in OS4?
NickDK
Nevermind...I wasn't calling sychronize on NSUserdefaults.
NickDK
A: 

This is to be expected the multitasking bar "red close button" is the equivalent of force quitting an application on Mac OS X. This is the documented behavior. Apple expects normal users of iOS 4 to simply launch / use / dimiss apps they don't have to think in terms of terminating processes or anything. The system will manage that.

Just make sure to save any important data in application:willResignActive: or applicationWillEnterBackground. Because after that you might get killed when memory pressure forces the system to clean up... Snow Leopard introduced a similar behavior for apps that use the NSSupportsSuddenTermination key in their Info.plist. An app basically tells the system that it periodically saves its data bit by bit, so that when it goes in background there's nothing left to do or very little. Unlike big monolithic apps desktop apps...

François P.
Thanks alot. Check out my comment on the answer below. I'm having an issue saving state using NSUserDefaults as well now.
NickDK
A: 

Hi! Getting a program to work in iOS4 with applicationDidEnterBackground is ok. But I need to make sure that my program runs well on iOS3 as well. I suppose the code in applicationDidEnterBackground only works on iOS4, so that I really need to use applicationWillTerminate for iOS3 devices. So how to test the software properly using only iOS3 behaviour? Can I download an OS3 simulator somewhere or something? Other options? Thanks, Finn Ove

folium