5

How programmatically restart an iPhone app in iOS?

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

But may be something simple.

5
  • 10
    No. Don't do this. For the love of god. Commented Nov 21, 2010 at 18:08
  • Well the example you have quote is for Cocoa Application not for Cocoa Touch (iOS) application. Secondly its not possible on iOS to restart you app. Commented Nov 21, 2010 at 21:09
  • 3
    And, of course, the real question: Why do you want to?
    – tc.
    Commented Nov 21, 2010 at 22:22
  • I've updated my post to explicitly say that it's not for the iOS. I don't get why you'd even want to relaunch an iPhone app though.
    – Pripyat
    Commented Nov 23, 2010 at 14:14
  • @Pripyat - app relaunch could be required to ensure security in complex SDKs like AWS, where switching configurations (e.g. logout & login again), requires an app restart for changes to take affect e.g. The right way to clear AWSCognitoCredentialsProvider Commented Apr 9, 2021 at 0:47

4 Answers 4

21

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.

2
  • 3
    If you're feeling perverse, you can host the page from your app itself (you get about 10 seconds to exit in -applicationWillTerminate:; it might be easier to serve it in another thread, but you can equally just call socket(), bind(), listen(), accept(), write(), and close()). Let your app terminate after you've served the page and make the page launch your app with a delay (the "Refresh" or "Reload" header, I think).
    – tc.
    Commented Nov 21, 2010 at 22:27
  • You don't even need to set UIApplicationExitsOnSuspend; you just have to call exit(). Wrapping the "server" in -beginBackgroundTaskWithExpirationHandler: ensures that your app gets enough CPU time to serve the thing, and then you can sleep for 500 ms (to "ensure" the data is sent) and exit(). The complicated bit is parsing the request, but you can fudge that by sleeping for 500 ms and reading into a largeish (8K) buffer, or (if you're feeling adventurous) select+read for 500 ms.
    – tc.
    Commented Sep 8, 2011 at 14:55
1

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.

0

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.

6
  • 2
    I think Alexei refers to restarting the app, not the device. Of course, restarting the latter is more than out of the question. Commented Nov 21, 2010 at 17:35
  • Sure you can — you can call exit() or kill() or just return from main(), or various other things (like abort()). It's undistinguishable from a crash and will probably get your app rejected by Apple for that reason.
    – tc.
    Commented Nov 21, 2010 at 18:05
  • What means return from main()? I solve problem adding exit() button to app. I need it because measure first time and second time always different. Commented Nov 22, 2010 at 6:46
  • I'm using exit() to quit an app, got past app store review no problem. Commented Jan 31, 2012 at 11:52
  • 1
    @samvermette They can't have failed to notice it - if you enter a dob that makes you less than 18 on the initial screen it exits (requirement from the drinks company whose app it is.) I wouldn't be surprised if I submitted the same app and have it rejected on that though. Rejections all seem a bit random! Commented Feb 1, 2012 at 12:11
-1

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
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.