ios - 在应用程序处于后台状态时进行服务调用

标签 ios swift

当应用程序处于后台状态时,我必须每 30 秒访问一次服务器。如果我在 applicationDidEnterBackground 方法中编写了代码,它只会被调用一次,但我需要在应用程序处于后台时每 30 秒连续访问一次服务器。

最佳答案

请使用HSLocationManager为您的要求。我在我的一个项目中实现了相同的要求

Location manager that allows getting background location updates every n seconds with desired location accuracy.

Advantage:

  • OS will never kill our app if the location manager is currently running.

  • Give periodically location update when it required(range is between 2 - 170 seconds (limited by max allowed background task time))

  • Customizable location accuracy and time period.

  • Low memory consumption(Singleton class)

检索位置的默认时间为 30 秒,准确度为 200。

static let timeInternal = 30
static let accuracy = 200

更新:

是的,您可以通过在 didUpdateLocations 方法中编写 API 调用来实现。

func scheduledLocationManager(_ manager: HSLocationManager, didUpdateLocations locations: [CLLocation]) {
   logh("Make API Call here...")
}

关于ios - 在应用程序处于后台状态时进行服务调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56404372/

相关文章:

ios - 当字段为空时为 UISearchBar 启用 'search' 按钮? (IOS 7)

ios - iOS 是否会依赖另一个区域本地化字符串?

ios - 无法使用 swift 在 iPhone 6 中注册远程通知

swift - Swift 3 中的通用类型别名?

ios - 如何使用多个 NSPredicates 进行多个搜索值

带有子元素的 ios UITableViewController 元素

ios - 使 CollectionView Cell 水平单元格比其他单元格大

xcode - 使用 swift 文件时使用未声明的类型

swift - 不重复地加载数据和追加

swift - 在全局方法中创建 UIAlertController