在 iOS 4.0 中退出应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3097244/
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 17:22:40  来源:igfitidea点击:

Exit application in iOS 4.0

iosios4

提问by Tamer

Before iOS4.0 clicking the home button on iPhone exits the application, and Apple had in their guide that programmatically exiting the application was not accepted.

在 iOS4.0 单击 iPhone 上的主页按钮退出应用程序之前,Apple 在他们的指南中指出不接受以编程方式退出应用程序。

now everything changed in iOS4.0, clicking the home button puts your app in a suspended mode (multitasking).. and I think there should be a clear way for the user to exit the app, like an exit button.

现在一切都在 iOS4.0 中发生了变化,单击主页按钮会将您的应用程序置于暂停模式(多任务处理)......我认为应该有一种明确的方式让用户退出应用程序,例如退出按钮。

is it now OK with apple? and how can it be done?

苹果现在可以了吗?怎么做?

采纳答案by Eiko

No still shouldn't do this.

不仍然不应该这样做。

You have handlers for the different stages, so this is how you should do it. There's no point in exiting manually. If you restart the app, ideally it would start where you left off, so this is either by resuming or by starting and loading the old state.

你有不同阶段的处理程序,所以你应该这样做。手动退出没有意义。如果您重新启动应用程序,理想情况下它会从您离开的地方开始,因此这是通过恢复或启动并加载旧状态。

No reason for exit.

没有理由退出。

Edit

编辑

As this keeps popping up again: iOS Human Interface Guidelinessays "Don't Quit Programmatically". And we have seen many reports of apps that had calls to exit() in them in the past.

随着这再次出现:iOS 人机界面指南说“不要以编程方式退出”。我们已经看到许多关于应用程序在过去调用 exit() 的报告。

Exiting instead of suspending by setting the appropriate key in the Info.plist file is perfectly fine, of course - but that's not a dedicated UI Button, just application-specific implementation of program exit by the home button.

当然,通过在 Info.plist 文件中设置适当的键来退出而不是挂起是完全没问题的——但这不是一个专用的 UI 按钮,只是通过主页按钮退出程序的特定于应用程序的实现。

回答by Joost Schuur

You can set the Info.plist key UIApplicationExitsOnSuspend to make sure the app is completely terminated.

您可以设置 Info.plist 键 UIApplicationExitsOnSuspend 以确保应用程序完全终止。

回答by Stanislav Kutil

There's a reason for programmatically invoked exit().

以编程方式调用是有原因的exit()

Suppose you have a voip app that is always started at boot and restarted when killed by the system e.g. when memory warning happens. Normally it's preferred behavior because you need to run in background in order to maintain your voip TCP sockets.

假设您有一个 voip 应用程序,它总是在启动时启动,并在被系统杀死时重新启动,例如发生内存警告时。通常这是首选行为,因为您需要在后台运行以维护您的 voip TCP 套接字。

However, if the app supports multiple modes of operation — like a) run in background using TCP, and b) do not run in background but start only after accepting PUSH notification —, if the user uses the app in b) mode he doesn't fancy that the app is consuming memory that can be used for other apps.

但是,如果应用程序支持多种操作模式——例如 a) 在后台使用 TCP 运行,以及 b) 不在后台运行,而是仅在接受 PUSH 通知后启动——如果用户在 b) 模式下使用应用程序,他不会”不知道该应用程序正在消耗可用于其他应用程序的内存。

So it would be nice if the app could check upon the start if it was started to the background and user wanted the app to run in b) mode and gracefully exit(0)so it's no longer automatically restarted.

因此,如果应用程序可以在启动时检查它是否在后台启动并且用户希望应用程序在 b) 模式下正常运行,那么它会很好,exit(0)因此它不再自动重新启动。

回答by jww

Also see iOS Debugging Magic (Technical Note TN2239):

另请参阅iOS 调试魔术(技术说明 TN2239)

Be mindful that the iOS application lifecycle is under user control, meaning that iOS applications should not just quit. Your release build should only call abort in circumstances where it would have crashed anyway, and the abort call prevents damage to user data or allows you to more easily diagnose the problem.

请注意,iOS 应用程序生命周期受用户控制,这意味着 iOS 应用程序不应该只是退出。您的发布版本应该只在它无论如何都会崩溃的情况下调用 abort,并且 abort 调用可以防止损坏用户数据或允许您更轻松地诊断问题。

While on the topic of determining the cause for premature exit, Understanding and Analyzing iPhone OS Application Crash Reports (Technical Note TN2151)might be of interest.

在确定过早退出的原因的主题上,了解和分析 iPhone OS 应用程序崩溃报告(技术说明 TN2151)可能会引起关注。

Sorry for going off topic a bit, but it relates to early exits and diagnosis.

抱歉有点跑题了,但这与早期退出和诊断有关。

Jeff

杰夫

回答by Frederick C. Lee

You can always use exit(1). This is a raw/forced exit with an integer for a code/reason. You can used this during development, like in simulation mode when you just want to terminate; as in NOW.

您始终可以使用exit(1). 这是出于代码/原因而带有整数的原始/强制退出。您可以在开发过程中使用它,就像在您只想终止的模拟模式下一样;就像现在一样。

回答by iMeMyself

Here are the steps for @Joost Schuur;s answer:

以下是@Joost Schuur 的回答步骤:

  1. Open your info.plistfile

  2. Add The Key UIApplicationExitsOnSuspend or Select Application does not run in background

  3. Set the new key to YES or Fill in the tick box

  1. 打开您的info.plist文件

  2. 添加密钥UIApplicationExitsOnSuspend 或 Select Application 不在后台运行

  3. 将新密钥设置为YES 或填写复选框

Breakpoints in applicationWillResignActiveand applicationWillTerminatewill show it works.

在断点applicationWillResignActive,并applicationWillTerminate会显示它的作品。

回答by Yoichi

I had a real problem with this. There is a big point in exiting manually or prgramatically.

我有一个真正的问题。手动或以编程方式退出有很大的意义。

With previous iPhone OS, my app was writing out its state (first use or second time onwards, etc.) in a plist when it terminated. When the user came back, it wanted to show different things by reading the plist. Also, it wanted to show the first screen every time when the user came back after exiting.

使用以前的 iPhone 操作系统,我的应用程序在终止时会在 plist 中写出它的状态(第一次使用或第二次以后等)。当用户回来时,它想通过阅读 plist 来展示不同的东西。此外,它希望每次用户退出后返回时都显示第一个屏幕。

With app becoming suspended in the background with iPhone OS4, the app comes back where it left off (i.e. showing the same screen wherever the user was on) and never changes the state of it, because applicationWillTerminate is now never called.

随着应用程序在 iPhone OS4 中暂停在后台,应用程序会从停止的地方返回(即在用户打开的任何地方显示相同的屏幕)并且永远不会改变它的状态,因为 applicationWillTerminate 现在永远不会被调用。

Becasue this is the behaviour desired most of the time (to be able to continue when you step out of the app temporary), there has to be a way to be able to choose, i.e. suspend it or quit.

因为这是大多数时候需要的行为(为了能够在您暂时退出应用程序时继续),必须有一种方法可以选择,即暂停或退出。

Since setting the UIApplicationExitsOnSuspend=YES gives only one way (i.e. it always terminates when the HOME is pressed), this is not a solution I am looking for.

由于设置 UIApplicationExitsOnSuspend=YES 只提供一种方式(即它总是在按下 HOME 时终止),这不是我正在寻找的解决方案。

I want the app to know once the whole chain of steps are completed, opposed to just the sequence was suspended, and to quit itself at the right time.

我希望应用程序知道一旦整个步骤链完成,而不是仅仅暂停序列,并在正确的时间退出。

To do this, I have to be able to terminate the app and write out the state once the use completed the entire sequence. Other times, I just want the app to be suspended.

为此,我必须能够在使用完成整个序列后终止应用程序并写出状态。其他时候,我只想暂停应用程序。

If you tap the HOME button twice you can see the suspended apps. I can delete (quit) my app by touching it longer and touch the (-) symbol that comes up, but this is not so intuitive for the users and too many steps.

如果您点击 HOME 按钮两次,您可以看到暂停的应用程序。我可以通过触摸更长时间并触摸出现的 (-) 符号来删除(退出)我的应用程序,但这对用户来说不是那么直观,而且步骤太多。

Another option is to have a Quit button as one of the Nav Tabs in my app, but that is ugly. For now, my only option seems to be opting to set the UIApplicationExitsOnSuspend=YES.

另一种选择是在我的应用程序中将退出按钮作为导航选项卡之一,但这很难看。目前,我唯一的选择似乎是选择设置 UIApplicationExitsOnSuspend=YES。

回答by bobobobo

You're not supposed to do this. When the user wants to quit your app, he will press the HOME button.

你不应该这样做。当用户想要退出你的应用时,他会按下 HOME 键。

When the user pushes the home button, you get a UIApplicationWillResignActiveNotificationnotification. Use that notification to tear down all your resources.

当用户按下主页按钮时,您会收到UIApplicationWillResignActiveNotification通知。使用该通知来拆除您的所有资源。

Basically your app should "hide in a corner" (consume as little memory as possible) when the user pushes the home button. However, there is a tradeoff as the more you tear down, the longer it takes for the app to be shown again when the user switches back (need to reload all your resources).

基本上,当用户按下主页按钮时,您的应用程序应该“隐藏在角落里”(尽可能少地消耗内存)。然而,有一个权衡,因为你拆的越多,当用户切换回来时,应用程序再次显示的时间就越长(需要重新加载你的所有资源)。

回答by COVID19

Just use below method to gracefully exit with animation

只需使用下面的方法优雅地退出动画

   @IBAction func MinimizeOrKillApp(){            
        UIControl().sendAction(#selector(URLSessionTask.suspend), to: UIApplication.shared, for: nil)
        //Comment if you want to minimise app
        Timer.scheduledTimer(withTimeInterval: 0.2, repeats: false) { (timer) in
            exit(0)
        }
    }

Output

输出

Download sample code

下载示例代码

It is not recommended and your app will be rejected. We all are the developers and We know to how to approve it from reviewer team

不推荐这样做,您的应用程序将被拒绝。我们都是开发人员,我们知道如何从审阅者团队批准它

Apple developer question

苹果开发者问题

回答by Deepak Kumar

You can do this:

你可以这样做:

@IBAction func yourButtonAcation(_ sender: Any) {

    UIControl().sendAction(#selector(URLSessionTask.suspend), to: UIApplication.shared, for: nil)

}