How programmatically restart an iPhone app in iOS

23,767

Solution 1

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.

Solution 2

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.

Share:
23,767

Related videos on Youtube

Andrei Eremchuk
Author by

Andrei Eremchuk

Updated on July 09, 2022

Comments

  • Andrei Eremchuk
    Andrei Eremchuk almost 2 years

    How programmatically restart an iPhone app in iOS?

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

    But may be something simple.

    • Jonathan Sterling
      Jonathan Sterling over 13 years
      No. Don't do this. For the love of god.
    • tc.
      tc. over 13 years
      And, of course, the real question: Why do you want to?
  • Adrian Kosmaczewski
    Adrian Kosmaczewski over 13 years
    I think Alexei refers to restarting the app, not the device. Of course, restarting the latter is more than out of the question.
  • tc.
    tc. over 13 years
    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.
    tc. over 13 years
    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).
  • Andrei Eremchuk
    Andrei Eremchuk over 13 years
    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.
  • tc.
    tc. over 12 years
    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.
  • Michael Behan
    Michael Behan over 12 years
    I'm using exit() to quit an app, got past app store review no problem.
  • samvermette
    samvermette over 12 years
    @mbehan probably because the review team didn't stumble upon it. If they had they most likely would have rejected it.
  • Michael Behan
    Michael Behan over 12 years
    @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!