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.
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.