views:

97

answers:

3

Hallo Everyone,

with the iOS 4, the iPhone is supporting Multitasking, what is very nice, but something I do not wish to support in my Application. I mean, when the user press the Home-button, I want my application to finish and not to enter in Background. With the iOS 4, when the User press the Home-button, the App calls the applicationDidEnterBackground: delegate's method to enter in Background and in order to "force" the Application to finish when the user press the Home button, I've done following implementation:

- (void)applicationDidEnterBackground:(UIApplication *)application {
        //save everything...
    exit(0);
}

PROBLEM: I've noticed, that exit(0) brings the Application immediately to finish, without calling deallocating methods like "dealloc", and I think that is not a good programming style. So I would like to ask you guys, how to bring the application to finish in a "nicer" way.

Thanks in advance.

A: 

You are not allowed to. I know from experience: got an app rejection from Apple because I've exited (that was two and a half years ago but I doubt they've changed their policy here). There's a "private" (i.e. not mentioned in header file) method "terminate" on UIApplication, IIRC. But Apple says you may not do that. Only thing you can do is to show a dialog, asking the user to press the home button. But in turn doesn't work if on a device with multitasking enabled... so I guess you really have to change your application in such a way that you can throw away your state on applicationDidEnterBackground and start afresh on application on applicationDidBecomeActive.

DarkDust
Actually, we are allowed to exclude apps from backgrounding functionality in iOS 4 - it's a matter of how exactly to do it. See the dupe questions linked above.
BoltClock
I have seen two games so far with exit buttons, and I really hate the programs for that. But you don't have to support multitasking.
Eiko
+2  A: 

That's two questions:

  1. How to programmatically exit an iPhone app - duplicate

    http://stackoverflow.com/questions/355168/proper-way-to-exit-iphone-application

  2. How to cause an iPhone app to not go to background in iOS4:

    Add the UIApplicationExitsOnSuspend key to your info.plist and set its value to YES http://developer.apple.com/iphone/library/documentation/General/Reference/InfoPlistKeyReference/Articles/iPhoneOSKeys.html#//apple_ref/doc/uid/TP40009252-SW23

Pumbaa80
+3  A: 

What you actually want is not to exit the application (which is as mentioned not allowed), but tell the OS you would rather your application be killed rather than backgrounded.

There is an info.plist key for that, UIApplicationExitsOnSuspend. Set that in your info.plist to TRUE (checked) and then when your app enters the background it will be terminated.

Kendall Helmstetter Gelner
This is correct, but really though, why wouldn't you want to support at least fast app switching? It helps the user, and isn't hard to implement. Remember, fast app switching doesn't take up CPU cycles or waste the battery. The app isn't running, it's just frozen in memory. Let the OS decide when to kill it off entirely. Its smart enough about that.
Joost Schuur
Thanks Helmstetter and Pumbaa80 for your answer, you gave me a better answer than I was expecting for. And yes, this boolean value on the Info.plist worked for me.@Joost Schuur: yes, I could, but it would not change everything: when the program comes active or "has finished loading", it must upload an xml from the server to shows it contents. So, implementing multitasking would not be a big deal. Multitasking depens on your goals and design pattern.
jcdmb
You should stop thinking of it as, and calling it 'multitasking'. Fast app switching is about improving the responsiveness of applications when you return back to it after you've side tracked to another app. How long until this XML data you load up is out of date? If someone load your app, spends 30 seconds in it, checks an email and then comes back to it, can you not use the old copy of the data? Why reload it again when the app restarts from scratch? It's not about running an app in the background. It's about the startup experience.
Joost Schuur