ios 退出iPhone应用程序的正确方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/355168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:26:19  来源:igfitidea点击:

Proper way to exit iPhone application?

iosobjective-ciphonecocoa-touchipad

提问by user21293

I am programming an iPhone app, and I need to force it to exit due to certain user actions. After cleaning up memory the app allocated, what's the appropriate method to call to terminate the application?

我正在编写一个 iPhone 应用程序,由于某些用户操作,我需要强制它退出。清理应用程序分配的内存后,调用终止应用程序的适当方法是什么?

采纳答案by Brett

Have you tried exit(0)?

你试过exit(0)吗?

Alternatively, [[NSThread mainThread] exit], although I have not tried that it seems like the more appropriate solution.

或者,[[NSThread mainThread] exit]虽然我没有尝试过它似乎是更合适的解决方案。

回答by August

On the iPhone there is no concept of quitting an app. The only action that should cause an app to quit is touching the Home button on the phone, and that's not something developers have access to.

在 iPhone 上没有退出应用程序的概念。应该导致应用程序退出的唯一操作是触摸手机上的主页按钮,而这不是开发人员可以访问的。

According to Apple, your app should not terminate on its own. Since the user did not hit the Home button, any return to the Home screen gives the user the impression that your app crashed. This is confusing, non-standard behavior and should be avoided.

根据 Apple 的说法,您的应用不应自行终止。由于用户没有点击主页按钮,因此任何返回主页屏幕都会给用户留下您的应用程序崩溃的印象。这是令人困惑的、非标准的行为,应该避免。

回答by MaxEcho

exit(0) appears to a user as crashes, so show a confirmation message to user. After confirmation suspend(home button press programmatically) and wait 2 seconds while app is going background with animation then exit behind user's view

exit(0) 向用户显示崩溃,因此向用户显示确认消息。确认挂起后(以编程方式按下主页按钮)并等待 2 秒钟,而应用程序正在以动画背景运行,然后退出用户视图

-(IBAction)doExit
{
    //show confirmation message to user
    UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Confirmation"
                                                 message:@"Do you want to exit?"
                                                delegate:self
                                       cancelButtonTitle:@"Cancel"
                                       otherButtonTitles:@"OK", nil];
    [alert show];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex != 0)  // 0 == the cancel button
    {
        //home button press programmatically
        UIApplication *app = [UIApplication sharedApplication];
        [app performSelector:@selector(suspend)];

        //wait 2 seconds while app is going background
        [NSThread sleepForTimeInterval:2.0];

        //exit app when app is in background
        exit(0);
    }
}

回答by Wagh

Check the Q&A here: https://developer.apple.com/library/content/qa/qa1561/_index.html

在此处查看问答:https: //developer.apple.com/library/content/qa/qa1561/_index.html

Q: How do I programmatically quit my iOS application?

There is no API provided for gracefully terminating an iOS application.

In iOS, the user presses the Home button to close applications. Should your application have conditions in which it cannot provide its intended function, the recommended approach is to display an alert for the user that indicates the nature of the problem and possible actions the user could take — turning on WiFi, enabling Location Services, etc. Allow the user to terminate the application at their own discretion.

WARNING:Do not call the exitfunction. Applications calling exitwill appear to the user to have crashed, rather than performing a graceful termination and animating back to the Home screen.

Additionally, data may not be saved, because -applicationWillTerminate:and similar UIApplicationDelegatemethods will not be invoked if you call exit.

If during development or testing it is necessary to terminate your application, the abortfunction, or assertmacro is recommended

问:如何以编程方式退出我的 iOS 应用程序?

没有提供用于正常终止 iOS 应用程序的 API。

在 iOS 中,用户按下 Home 键关闭应用程序。如果您的应用程序存在无法提供其预期功能的情况,推荐的方法是向用户显示警报,指示问题的性质以及用户可能采取的操作——打开 WiFi、启用定位服务等。允许用户自行决定终止应用程序。

警告:不要调用该exit函数。调用的应用程序exit将在用户看来已崩溃,而不是执行正常终止并以动画方式返回主屏幕。

此外,数据可能不会被保存,因为如果您调用 exit,将不会调用-applicationWillTerminate:类似的UIApplicationDelegate方法。

如果在开发或测试期间需要终止您的应用程序,建议使用该abort函数或assert

回答by Wagh

Its not really a way to quit the program, but a way to force people to quit.

它不是真正的退出程序的方法,而是一种强迫人们退出的方法。

UIAlertView *anAlert = [[UIAlertView alloc] initWithTitle:@"Hit Home Button to Exit" message:@"Tell em why they're quiting" delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
[anAlert show];

回答by Kalyan

Go to your info.plist and check the key "Application does not run in background". This time when the user clicks the home button, the application exits completely.

转到您的 info.plist 并检查键“应用程序不在后台运行”。这一次当用户单击主页按钮时,应用程序完全退出。

回答by Aman Agarwal

Add UIApplicationExitsOnSuspendproperty on application-info.plistto true.

UIApplicationExitsOnSuspend属性添加application-info.plisttrue.

回答by rchampourlier

After some tests, I can say the following:

经过一些测试,我可以说以下几点:

  • using the private interface : [UIApplication sharedApplication]will cause the app looking like it crashed, BUT it will call - (void)applicationWillTerminate:(UIApplication *)applicationbefore doing so;
  • using exit(0);will also terminate the application, but it will look "normal" (the springboard's icons appears like expected, with the zoom out effect), BUT it won't call the - (void)applicationWillTerminate:(UIApplication *)applicationdelegate method.
  • 使用私有接口:[UIApplication sharedApplication]会导致应用看起来像是崩溃了,但它会- (void)applicationWillTerminate:(UIApplication *)application在这样做之前调用;
  • usingexit(0);也将终止应用程序,但它看起来“正常”(跳板的图标看起来像预期的那样,具有缩小效果),但它不会调用- (void)applicationWillTerminate:(UIApplication *)application委托方法。

My advice:

我的建议:

  1. Manually call the - (void)applicationWillTerminate:(UIApplication *)applicationon the delegate.
  2. Call exit(0);.
  1. 手动调用- (void)applicationWillTerminate:(UIApplication *)application委托。
  2. 打电话exit(0);

回答by L'g

Your ApplicationDelegate gets notified of intentional quitting by the user:

您的 ApplicationDelegate 会收到用户有意退出的通知:

- (void)applicationWillResignActive:(UIApplication *)application {

When I get this notification I just call

当我收到此通知时,我只需致电

        exit(0);

Which does all the work. And the best thing is, it is the useres intent to quit, which is why this should not be a problem calling it there.

它可以完成所有工作。最好的事情是,用户打算退出,这就是为什么在那里调用它不应该是问题的原因。

On my Audio-App it was necessary to quit the app after people were syncing their device while the music was still playing. As soon as the syncing is complete I get a notification. But quitting the app right after that would actually look like a crash.

在我的音频应用程序上,当人们在音乐仍在播放时同步他们的设备后,有必要退出应用程序。同步完成后,我会收到通知。但在那之后立即退出应用程序实际上看起来像是崩溃。

So instead I set a flag to REALLY quit the app on the next backgrounding action. Which is okay for refreshing the app after a sync.

因此,我设置了一个标志,以便在下一个后台操作时真正退出应用程序。同步后刷新应用程序是可以的。

回答by AGPX

Apple say:

苹果说:

"Warning: Do not call the exit function. Applications calling exit will appear to the user to have crashed, rather than performing a graceful termination and animating back to the Home screen."

“警告:不要调用 exit 函数。调用 exit 的应用程序在用户看来会崩溃,而不是执行正常终止并动画返回主屏幕。”

I think that this is a bad assumption. If the user tap a quit button and a message appears that say something like: "The application will now quit.", it doesn't appear to be crashed. Apple should provide a valid way to quit an application (not exit(0)).

我认为这是一个糟糕的假设。如果用户点击退出按钮并出现一条消息,内容类似于:“应用程序现在将退出。”,它似乎没有崩溃。Apple 应该提供一种有效的方式来退出应用程序(而不是 exit(0))。