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 应用程序?
基础教程推荐
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
