Is there a way to force an ios 4 app to close and re-open itself?
开发者_高级运维The app that I'm currently working on has a problem propagating settings, but seems to work fine once it's been closed and re-opened. It's an alarm clock application so it doesn't benefit from multi-tasking. Since I am using NSUserDefaults to save pertinent settings, I have shut off multitasking:
@implementation MyAppDelegate
- (void) applicationDidEnterBackground:(UIApplication *)application {
exit(0);
}
...
@end
So at this point the user has to manually close and re-open the app. Is there a command I can send to cause the app to close and re-open (automatically)? I would like the app to close and re-open if certain settings are changed when the settings view controller is removed from the screen.
Rather than forcing your application to close on exit, which is a terrible user experience, you should address the root of your problem.
If you are having problems with settings being made in your application that are refusing to propagate, try using
[[NSUserDefaults standardUserDefaults] synchronize];
to flush the settings to disk.
If the issue that you are running into is that settings are being changed in the Settings application while your application is in the background, add an observer in your application that listens for changes in these settings:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleChangeInUserSettings:) name:NSUserDefaultsDidChangeNotification object:nil];
which will be triggered when your application returns to the foreground if something has changed. Within this method, update your user interface or other aspects of your application that need to be adjusted with the updated settings.
No, you cannot force an app to restart itself. Using exit()
in your code violates Apple HIG.
To accomplish what you want, I suggest showing an alert to the user when the settings are changed, which will advise then that the settings will take effect on next launch.
You should disable multitasking by adding UIApplicationExitsOnSuspend to your info.plist and set it to true.
精彩评论