3

or "How to simulate a home button pressed event?"

I need to restart my iPhone app, and I want the program to quit, so the user will only have to start it.

If I simply use exit(0) some changes won't get saved, as they would if the user quits by pressing the home button.

The restart needed for language change.

Related code:

- (void)onChangeLanguage: (id)sender {
    NSArray *lang = [NSArray arrayWithObjects:((Whatever *)sender).newLanguage, nil];
    [[NSUserDefaults standardUserDefaults] setObject:lang forKey:@"AppleLanguages"];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSArray *languages = [defaults objectForKey:@"AppleLanguages"];
    NSString *currentLanguage = [languages objectAtIndex:0];

    NSLog(@"Current language: %@", currentLanguage);
    // ***
}   

If the user restarts using the home button, language will change.

If // *** is replaced by exit(0), the language won't change.

shinjin
  • 2,858
  • 5
  • 29
  • 44
  • You don't need to restart for a language change if you force NSUserDefaults to use a specific bundle. See http://stackoverflow.com/questions/1669645/how-to-force-nslocalizedstring-to-use-a-specific-language/1746920#1746920 – Jano Jul 06 '11 at 22:53

3 Answers3

7

Calling exit(0) is the only legal (but highly not recommended) way to exit the program from your code.
As a possible workaround you can show UIAlertView with no buttons that cannot be dismissed (forcing user to quit your program manually) and telling the user that he has to do that to apply your changes.

Vladimir
  • 170,431
  • 36
  • 387
  • 313
  • Why is calling `exit` not recommended? – zoul Nov 10 '09 at 14:07
  • Seems like bad interface design to me. – MattC Nov 10 '09 at 14:14
  • Aha, so it’s not wrong from the engineering point of view? I agree that quitting the application unexpectedly is unfair to the user, but other times it’s perfectly valid – for example if you have a Quit button. (Which, for whatever reason, many applications do.) – zoul Nov 10 '09 at 14:23
  • 4
    Quote from apple HIG: "iPhone applications should never quit programmatically because doing so looks like a crash to the user. Theremaybetimes, however, when external circumstances prevent your application from functioning as intended. When this happens, you need to tell users about the situation and explain what they can do about it. Thisway, users decide whether they want to take corrective action and continue with your application or press the Home button and open a different application." – Vladimir Nov 10 '09 at 15:02
  • I agree that showing this alert is ugly... Its also possible just show an alert note (disposable) explaining to user that the changes will not be applied until he restarts the application – Vladimir Nov 10 '09 at 15:03
5

I think it’s perfectly fine to call exit, just call [[NSUserDefaults standardUserDefaults] synchronize] before you do that. You can read about the synchronize method in the Apple Documentation:

Because this method is automatically invoked at periodic intervals, use this method only if you cannot wait for the automatic synchronization (for example, if your application is about to exit) or if you want to update the user defaults to what is on disk even though you have not made any changes.

zoul
  • 102,279
  • 44
  • 260
  • 354
3

It's worth noting that there's a private API call, too. Of course, all the usual warnings about using private APIs apply.

[[UIApplication sharedApplication] terminate];

Jeremy Bower
  • 576
  • 4
  • 7
  • Oookay, and what is "the usual warnings about using private APIs"? – shinjin Nov 17 '09 at 09:36
  • The usual warning is that (1) your application could be rejected from the App store and (2) the private APIs are not guaranteed to stay stable, which means your app could easily break with new firmware. – zoul Nov 17 '09 at 14:04
  • Exactly. And things have gotten more treacherous now that Apple is scanning for apps which use private API calls to reject them from the app store. Using a private API is not a good option, but exit(0) isn't quite right either (the app should fire notifications first). Showing an alert to ask the user to push the home button and relaunch the app might be the best way to go, and the most consistent with Apple's HIG. – Jeremy Bower Nov 22 '09 at 04:41