gpt4 book ai didi

ios - iOS 应用程序在后台时的 HTTP 连接,如果离线则重试

转载 作者:可可西里 更新时间:2023-11-01 03:56:07 28 4
gpt4 key购买 nike

我编写了一个将在后台运行的基本位置跟踪应用程序。 (不仅是后台线程,还有当设备处于黑暗状态时。)我设置了 UIBackgroundModes 键,这部分可以很好地将位置写入屏幕。

接下来,我需要处理这些位置。根据位置和其他一些标准,应用程序需要偶尔通过 HTTP get 或 post 向服务器发送一些信息。有一些挑战:

  1. 即使应用程序在后台,也需要将消息发送到服务器。

  2. 即使在生成消息时设备处于离线状态,也需要将消息发送到服务器(用户在获取 GPS 位置时通常会超出小区范围)。当然,不必立即发送消息,但应将它们存储起来,并在设备重新联机时发送。

  3. 如果消息没有通过第一次尝试,应该在放弃之前重新发送几次。

我正在寻求帮助来规划如何处理这三件事。

该应用程序将针对 iOS6+。

我是一名新的 iOS 开发人员,但我已经为 Android 开发了一个非常相似的应用程序。我正在寻找一般建议和策略 - 不是代码,除非碰巧已经有一些。我只是不太了解 iOS/Objective C,还不知道是否已经有类或框架来处理这个问题或帮助解决它的各个方面。我看过很多“如何将数据发送到服务器”的例子,但这不是我要找的。相反,我想要指出哪些类或系统可能有助于管理重试连接/命令、如何在后台处理它等等。此外,对需要注意的问题的警告也很有帮助。非常感谢您的任何建议。

最佳答案

您很快就会发现,当应用程序处于后台时,您能做什么和不能做什么会受到更多限制。您最好的资源将是 App States and Multitasking Guide在 iOS 开发者库中。来自文档:

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:

  • 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 updates from external accessories

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.

听起来您的应用可能符合位置项目符号项的条件。您可以通过在 info.plist 文件中包含 UIBackgroundModes 键来启用后台位置服务,并且您必须设计应用程序以与后台模式和后台获取(系统将提供的回调,在您的应用程序委托(delegate)中处理)。阅读多任务处理指南的“跟踪用户位置”部分。

此外,iOS 7 中还有一些新的注意事项需要研究。如果您可以访问成员(member)中心,我建议您查看 WWDC 视频以了解更多信息。

关于ios - iOS 应用程序在后台时的 HTTP 连接,如果离线则重试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18521895/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com