允许后台模式,但有条件终止?

发布于 2024-12-04 17:58:40 字数 365 浏览 0 评论 0原文

我将“应用程序不在后台运行”设置为“否”

,这意味着它可以进入后台并稍后恢复。

我将一些代码放入“applicationDidEnterBackground”中, 我想在需要时强制退出该应用程序。 (当后台无事可做时。)

我读到不建议“退出”。
我可以发布一条可能会导致 applicationWillTerminate 之类的消息吗?

  • 编辑

让我澄清一下为什么我想要这样, 我希望我的应用程序在下载某些内容时在后台运行..通过 beginBackgroundTaskWithExpirationHandler
但如果用户没有下载任何内容,我可以安全地退出该应用程序。

谢谢

I'm setting "Application does not run in background" to NO

which means, it can go background and resumed later.

I'm putting some code in "applicationDidEnterBackground",
and I would like to force quit the app when desirable. (when there's nothing to do in background.)

I read 'exit' is not recommended.
Can I post a message which will result in something like applicationWillTerminate maybe?

  • Edit

Let me clarify why I would want that,
I want my app to run background when it's downloading something.. via beginBackgroundTaskWithExpirationHandler
But if user isn't downloading anything, I can safely quit the application.

Thank you

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

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

发布评论

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

评论(2

愛上了 2024-12-11 17:58:40

Apple 在 iOS 上的一般做法是应用程序永远不应该因任何原因自行终止。

重要的是要了解,当您的应用程序在后台“运行”时,它并没有真正运行:“应用程序不在后台运行”键基本上是为了确保与为 iOS 3 及更低版本构建的应用程序向后兼容,这往往会导致对用户如何进入应用程序做出某些假设。新应用程序不支持它的原因很少。

基本上,不用费心:我想从技术上讲你可以调用 exit(),但苹果会因此讨厌你,而且这对于平台来说是不好的风格。

The general Apple approach on iOS is that applications should never terminate themselves, for any reason.

It's important to understand that when your application is 'running' in the background, it's not really running: the 'Application does not run in background' key is basically there to ensure backwards compatibility with applications built for iOS 3 and lower, which tend to make certain assumptions about how the user will enter the app. There are very few reasons why a new app wouldn't support it.

Basically, don't bother: I suppose you could technically call exit(), but Apple will hate you for it, and it's bad style for the platform.

⊕婉儿 2024-12-11 17:58:40

你不需要担心这个。

如果您在后台有工作要做,并且设置了后台任务,那么它将根据需要运行,并在完成后执行其处理程序。

您似乎没有意识到,如果您在后台无事可做,您的应用程序就会被挂起,实际上不会运行。

在这种情况下,您无需执行任何操作,退出应用程序不会为您带来任何暂停后无法获得的东西。

You don't need to worry about this.

If you have work to do in the background and you set up a background task then it'll run for as long as it needs and execute its handler when done.

What you don't seem to be aware of is that if you have nothing to do in the background, your app is simply suspended and in fact does not run.

As this is the case, there's nothing you need to do, quitting the app won't gain you anything that you don't already get with suspension.

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