Terminating App

Discussion in 'iOS Programming' started by robbieduncan, Aug 10, 2008.

  1. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #1
    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?
     
  2. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #2
    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.
     
  3. robbieduncan thread starter Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #3
    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 ;)
     
  4. robbieduncan thread starter Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    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...
     

Share This Page