How to quit an iPhone app nicely?(如何很好地退出 iPhone 应用程序?)
问题描述
或如何模拟主页按钮按下事件?"
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 应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何很好地退出 iPhone 应用程序?


基础教程推荐
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01