或如何模拟主页按钮按下事件?"
or "How to simulate a home button pressed event?"
我需要重新启动我的 iPhone 应用程序,并且我希望程序退出,因此用户只需启动它.
I need to restart my iPhone app, and I want the program to quit, so the user will only have to start it.
如果我只是使用 exit(0)
,一些更改将不会被保存,就像用户通过按主页按钮退出时一样.
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.
相关代码:
- (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.
如果 //***
被 exit(0)
替换,语言不会改变.
If // ***
is replaced by exit(0)
, the language won't change.
我觉得调用exit
就完全没问题了,之前调用[[NSUserDefaults standardUserDefaults] synchronize]
你做吧.您可以在 synchronize 方法.html#//apple_ref/occ/instm/NSUserDefaults/synchronize" rel="nofollow noreferrer">苹果文档:
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.
这篇关于如何很好地退出 iPhone 应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!