5

How programmatically restart an iPhone app in iOS?

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

But may be something simple.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Andrei Eremchuk
  • 155
  • 1
  • 1
  • 8
  • 10
    No. Don't do this. For the love of god. – Jonathan Sterling Nov 21 '10 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. – itsaboutcode Nov 21 '10 at 21:09
  • 3
    And, of course, the real question: Why do you want to? – tc. Nov 21 '10 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 Nov 23 '10 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](https://github.com/aws-amplify/aws-sdk-ios/issues/3473#issuecomment-801158276) – Lactose.Int.Robot Apr 09 '21 at 00:47

4 Answers4

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.

TomSwift
  • 39,369
  • 12
  • 121
  • 149
  • 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. Nov 21 '10 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. Sep 08 '11 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.

Pripyat
  • 2,937
  • 2
  • 35
  • 69
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.

samvermette
  • 40,269
  • 27
  • 112
  • 144
  • 2
    I think Alexei refers to restarting the app, not the device. Of course, restarting the latter is more than out of the question. – Adrian Kosmaczewski Nov 21 '10 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. Nov 21 '10 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. – Andrei Eremchuk Nov 22 '10 at 06:46
  • I'm using exit() to quit an app, got past app store review no problem. – Michael Behan Jan 31 '12 at 11:52
  • @mbehan probably because the review team didn't stumble upon it. If they had they most likely would have rejected it. – samvermette Jan 31 '12 at 21:17
  • 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! – Michael Behan Feb 01 '12 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
}
Jesse
  • 8,605
  • 7
  • 47
  • 57