定期更新位置后台Swift 3.0 iOS 10



我制作了一个应用程序,当它进入后台时,我需要向服务器发送一些定期数据。对于从后台唤醒,我使用后台更新重要位置(较低的电池)。我注意到,当手机从wifi/3g或3g改变蜂窝塔时,位置更新,但我的问题是,如果用户不移动(然后蜂窝塔不改变),位置不更新,应用程序不唤醒,然后我不能发送数据到服务器。

你知道解决这个问题的方法吗?

我在AppDelegate文件中做所有这些:

class AppDelegate: UIResponder, UIApplicationDelegate ,  CLLocationManagerDelegate{
var manager = CLLocationManager()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        manager.desiredAccuracy = kCLLocationAccuracyBest
        manager.delegate = self
        manager.requestAlwaysAuthorization()
        manager.allowsBackgroundLocationUpdates = true
        return true
    }
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){
      sendDataToServer()
}
func applicationDidEnterBackground(_ application: UIApplication) {
        manager.startMonitoringSignificantLocationChanges()
}
}

只有当发生位置变化(即蜂窝塔变化)时才触发重要的位置变化事件。如果你需要的是定期下载或上传数据,你应该使用后台取回而不是位置更改来唤醒应用程序:

application.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum)
然后

的处理
func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)

你试过了吗:

https://github.com/paleksandrs/APScheduledLocationManager

最新更新