Question

How programmatically restart an iPhone app in iOS?

I find this way http://writeitstudios.com/david/?p=54

But may be something simple.

Was it helpful?

Solution

The only way I know to do this is not ideal, but it works.

First, your app has to opt out of background execution (multitasking) The app has to quit when exited, not run as a background task. This is done with the plist key UIApplicationExitsOnSuspend.

Second, your app needs to register a custom URL scheme that can be used to launch the app.

Third, you need a web page hosted somewhere that when loaded will redirect to your app's custom URL scheme.

Forth, the user needs an active Internet connection.

To exit and restart, call UIApplication openURL on your hosted redirecting web page. Your app will exit and safari will launch and load your page. The page will redirect Safari to your custom URL scheme, prompting Safari to internally call openURL, causing iOS to launch your app.

OTHER TIPS

my post that you linked to is referring to a Cocoa Application, not the iOS. On the iOS, you can quit an application (but Apple doesn't like this) by using exit(0); but I don't recommend that. You cannot restart iPhone apps though.

Unless you're developing for jailbroken devices, Apple won't even allow you to programatically terminate your app. So restarting the device is out of the question.

Your AppDelegate instance has a method

(void)applicationDidBecomeActive:(UIApplication *)application
{
}

In here, you can put logic to figure out if the app should restart, or continue doing whatever it was doing. For example you can have a BOOL variable appMustRestart that is false at first but gets triggered as true whenever something happens in your app that you'd like the next time to be a fresh relaunch.

if (appMustRestart)
{
    [self resetVars];  // call a method that resets all your vars to initial settings

    // INSERT CODE HERE TO TRANSFER FOCUS TO INITIAL VIEWCONTROLLER
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top