当应用程序处于后台状态时,我必须每 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/