views:

81

answers:

3

There are some rare data-corruption circumstances where rather than attempt a recovery in the same session, I'd like to perform some fixes and then shutdown the app so the next launch will be safe.

Short of hard crashing the app with something dumb like *(unsigned int *)0 = 0xDEADBEEF, I can't find an API in Cocoa that causes a graceful shutdown.

UPDATE: Found this documentation on the subject, that essentially confirms suspicion and points to exit as a last-ditch option:

http://developer.apple.com/iphone/library/qa/qa2008/qa1561.html

+2  A: 
exit(0);

Yep it works on the iPhone too.

Alternatively,

[[UIApplication sharedApplication] terminate];
thyrgle
Fair enough, although I was hoping for something a little cleaner, like telling UIApplication to break out its run loop.
quixoto
Well, with a word like "forcibly". Either way its better than dead food or bad food.
thyrgle
Good point. Wording edited.
quixoto
`-terminate` is not a published method on UIApplication, so I'll steer clear. The exit(0) is validated by the docs here: http://developer.apple.com/iphone/library/documentation/uikit/reference/UIKitFunctionReference/Reference/reference.html#//apple_ref/c/func/UIApplicationMain
quixoto
That's a fine suggestion if you don't want your app accepted in the app store (there was no jailbreak tag on the story). Proceed at your own risk, and the rest of us thank you for reducing the amount of competition in the app store.
Kendall Helmstetter Gelner
A: 

When debugging a Debug build, I prefer something that will crash into the debugger so I'll not only know the reason why, but can poke around for more details if necessary. Sending a random message to any non-nil object is one Objective C way of doing this.

[ UIApplication foo: bar ];

For App store submission, -terminate will definitely get your app rejected for using an (officially) undocumented API. And exit(0) has been reported to do so also, even though it's a documented OS call.

Prior to OS 4.0, you could always send a URL to Safari, which would cause the OS to gracefully terminate your app. Not sure what to do in OS 4.0 and after that's App store legal.

hotpaw2
To terminate you app under iOS 4.0 via a Safari URL, you also need to add the UIApplicationExitsOnSuspend plist key. Sending the user to a web page that explains what happened is a much nicer touch than just having your app blink out.
hotpaw2
A: 

This doesn't make any sense to me - no resource should be out of reach for an internal reset. Force-closing the app sucks for the user.

Kendall Helmstetter Gelner
Are you commenting on my intentions?
quixoto
No, I am telling you that if you ever force close the app the user may just delete it and rate it one star. Not to mention if Apple figures it out they will not even accept the app in the first place. I'm just trying to help you avoid a needless shutdown that will cause you more pain than not.Since it's data corruption of some kind, the better approach would be on startup to clear out the data store before anything else happens.
Kendall Helmstetter Gelner
I have several apps that force close themselves under OS 3.1.x, and users seem to like being able to launch Safari to look up further info.
hotpaw2
I saw alert view cause an exit for "Word With Friends" when your not connected to the internet. I think that's a good cause.
thyrgle
Sorry, but that use case sucks. Let me quit the app - what if my connection was just spotty and I knew it was coming right back? Too bad for me, the app decided to crash itself. One star rating from me and many others for behavior like that.And again, you'll probably not make the app store.
Kendall Helmstetter Gelner