如何完美退出 iPhone 应用程序?

发布于 2024-08-11 05:14:15 字数 779 浏览 5 评论 0原文

或“如何模拟主页按钮按下事件?”

我需要重新启动我的 iPhone 应用程序,并且我希望该程序退出,因此用户只需启动它即可。

如果我只是使用 exit(0) ,某些更改将不会被保存,就像用户通过按主页按钮退出一样。

更改语言需要重新启动。

相关代码:

- (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);
    // ***
}   

如果用户使用主页按钮重新启动,语言将会改变。

如果将 // *** 替换为 exit(0),则语言不会改变。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北音执念 2024-08-18 05:14:15

调用 exit(0) 是从代码中退出程序的唯一合法(但强烈不推荐)的方法。
作为一种可能的解决方法,您可以显示没有无法关闭的按钮的 UIAlertView(强制用户手动退出程序),并告诉用户他必须这样做才能应用您的更改。

Calling exit(0) is the only legal (but highly not recommended) way to exit the program from your code.
As a possible workaround you can show UIAlertView with no buttons that cannot be dismissed (forcing user to quit your program manually) and telling the user that he has to do that to apply your changes.

乖乖 2024-08-18 05:14:15

我认为调用 exit 是完全可以的,只需在执行此操作之前调用 [[NSUserDefaults standardUserDefaults] Synchronize] 即可。您可以在 Apple 文档

因为这个方法是自动的
定期调用,使用
仅当您等不及时才使用此方法
用于自动同步(对于
例如,如果您的应用程序是关于
退出)或者如果您想更新
用户甚至默认磁盘上的内容
尽管您没有进行任何更改。

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.

沫尐诺 2024-08-18 05:14:15

值得注意的是,还有一个私有 API 调用。当然,所有有关使用私有 API 的常见警告都适用。

[[UIApplication共享应用程序]终止];

It's worth noting that there's a private API call, too. Of course, all the usual warnings about using private APIs apply.

[[UIApplication sharedApplication] terminate];

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文