在 iOS 中运行后台服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11417837/
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
Running background services in iOS
提问by Umair Khan Jadoon
I need to code an iOS app that sends data to server every 30 minutes. Can this work when the app is in the background? How reliable it can be?
我需要编写一个每 30 分钟向服务器发送一次数据的 iOS 应用程序。当应用程序在后台时,这可以工作吗?它有多可靠?
回答by Im_Lp
There is no way to perform tasks in the background permanently at the interval of time you are requesting. You may request specific permission via the developer connection but I must warn you that you will need a very compelling argument. I included the documentation below, maybe your request falls within one of the groupings that could run permanently. Or maybe you could use one of the long running background threads and adapt it in such a way that it fulfils the task you are attempting.
无法按照您请求的时间间隔在后台永久执行任务。您可以通过开发人员连接请求特定许可,但我必须警告您,您需要一个非常有说服力的论据。我包含了下面的文档,也许您的请求属于可以永久运行的分组之一。或者,也许您可以使用一个长时间运行的后台线程并对其进行调整,使其能够完成您正在尝试的任务。
Directly from Apple's Documentation:
直接来自 Apple 的文档:
Implementing Long-Running Background Tasks
实现长时间运行的后台任务
For tasks that require more execution time to implement, you must request specific permissions to run them in the background without their being suspended. In iOS, only specific app types are allowed to run in the background:
对于需要更多执行时间来实现的任务,您必须请求特定权限才能在后台运行它们而不会被挂起。在 iOS 中,只允许特定类型的应用程序在后台运行:
- Apps that play audible content to the user while in the background, such as a music player app
- Apps that keep users informed of their location at all times, such as a navigation app
- Apps that support Voice over Internet Protocol (VoIP) Newsstand apps that need to download and process new content
- Apps that receive regular update from external accessories
- 在后台向用户播放可听内容的应用程序,例如音乐播放器应用程序
- 让用户随时了解其位置的应用程序,例如导航应用程序
- 支持互联网协议语音 (VoIP) 报亭应用程序需要下载和处理新内容的应用程序
- 从外部配件接收定期更新的应用程序
Apps that implement these services must declare the services they support and use system frameworks to implement the relevant aspects of those services. Declaring the services lets the system know which services you use, but in some cases it is the system frameworks that actually prevent your application from being suspended.
实现这些服务的应用程序必须声明它们支持的服务并使用系统框架来实现这些服务的相关方面。声明服务让系统知道您使用了哪些服务,但在某些情况下,实际上是系统框架阻止了您的应用程序被挂起。
~/End of Line
〜/行尾
回答by hariszaman
In iOS7 new multitasking APIs are introduced due to which it is possible to run background task for infinite duration. Apple encourages to use NSURLSessionAPI which creates a background session and enqueues transfers
在 iOS7 中引入了新的多任务 API,因此可以无限期地运行后台任务。Apple 鼓励使用NSURLSessionAPI 来创建后台会话并将传输入队
The new UIBackgroundModeswhich include "Background Fetch" and "remote-notifications" are also introduced in iOS7 which help in running background services
iOS7中还引入了新的UIBackgroundModes,其中包括“Background Fetch”和“remote-notifications”,这有助于运行后台服务
回答by IgniteCoders
There's no way to do it as service except some special cases provided by ios, as https://stackoverflow.com/users/1515108/im-lpsaid. But you can simulate it. This post answer how to:
正如https://stackoverflow.com/users/1515108/im-lp所说,除了ios提供的一些特殊情况外,没有办法将其作为服务。但是你可以模拟它。这篇文章回答了如何:
https://stackoverflow.com/a/19121250/2835520
https://stackoverflow.com/a/19121250/2835520
I hope it's gonna help!
我希望它会有所帮助!