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

3

我正在制作一款应用程序,在后台运行时,我需要定期向服务器发送一些数据。为了唤醒它,我使用了后台更新显著位置(降低电量消耗)。我注意到当手机从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()
}

}

你得到了任何合理的解决方案吗? - Jacob Davis Cherussery
2个回答

2

只有在位置发生变化时(即基站更改)才会触发重要位置更改事件。如果您需要定期下载或上传数据,则应该使用后台获取而不是位置更改来唤醒应用程序:

application.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum)

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

0

我不明白你的方法是否使用了significantLocationChanged。因为我不能使用普通位置,只能使用电池低位置。 - alexander

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接