Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

robbieduncan

Moderator emeritus
Original poster
Jul 24, 2002
25,611
893
Harrogate
I've had a good look through the documentation but I can't find any way of doing what I want so here we go...

Is there any way to programatically terminate your iPhone app. I want to be able to kill the app if certain things go wrong (mostly parsing plists on startup). On the desktop we have the terminate: method of NSApplication, but I can't find a similar method in UIApplication.

Basically I want to be able to display and alert to the user explaining what has gone wrong (and suggest a reinstall to uncorrupt the file) and then terminate.

Any ideas?
 

firewood

macrumors G3
Jul 29, 2003
8,141
1,384
Silicon Valley
Try an exit(0) from your main().

Or you might just want to put up a modal dialog explaining that there was a problem, and leaving the user the option to go back to the launcher, or else to just watch your warning message forever because it's so informative.
 

robbieduncan

Moderator emeritus
Original poster
Jul 24, 2002
25,611
893
Harrogate
It's a good idea, but I'm not sure it's possible. My main (like most others I assume) starts a UIApplication like this:

Code:
int main(int argc, char *argv[]) {
	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	int retVal = UIApplicationMain(argc, argv, nil, @"DLR_DeparturesAppDelegate");
	[pool release];
	return retVal;
}

The error will occur during application startup somewhere in the UIApplicaitonMain call (sort of, really in a run loop). So I can't terminate here: I want to be able to cleanly terminate the UIApplication. It's not a huge issue: the plist I am parsing is shipped with the app and cannot be changed by the user. It's only an issue if the file somehow corrupts on the phone. I'm just trying to cover every unlikely scenario ;)
 

robbieduncan

Moderator emeritus
Original poster
Jul 24, 2002
25,611
893
Harrogate
OK, I've solved my own problem. This works (all be it with a compiler warning)

Code:
[[UIApplication sharedApplication] terminate];

Found out from here;

So UIApplication does have a terminate method, it's just not declared in the public header...

Edit: but of course it works in some places and not in others. The joys of using a non-public API...
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.