views:

1202

answers:

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.

+6  A: 

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
Why is calling `exit` not recommended?
zoul
Seems like bad interface design to me.
MattC
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
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
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
Thanks, I did not read that.
zoul
+2  A: 

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
Oookay, and what is "the usual warnings about using private APIs"?
shinjin
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
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
+4  A: 

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
That's it! Thanks, sync before exit solved my problem.
shinjin