【问题标题】:periodic update location background Swift 3.0 iOS 10定期更新位置后台 Swift 3.0 iOS 10
【发布时间】:2017-03-13 18:55:54
【问题描述】:

我制作了一个应用程序,当它进入后台时,我需要向服务器发送一些周期性数据。 为了从后台唤醒,我使用后台更新重要位置(电池电量较低)。 我注意到当手机从 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()
}

}

【问题讨论】:

  • 你有没有得到合理的解决方案?

标签: ios swift background location


【解决方案1】:

重大位置变化事件仅在位置发生变化(即信号塔变化)时触发。如果您需要定期下载或上传数据,您应该使用后台获取而不是位置更改来唤醒应用:

application.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum)

然后处理

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

【讨论】:

    【解决方案2】:

    你试过了吗:

    https://github.com/paleksandrs/APScheduledLocationManager

    后台任务最多 180 秒。

    【讨论】:

    • 我不明白您的方法是否使用显着LocationChanged。因为我不能使用正常位置,只能使用电池电量不足的位置
    猜你喜欢
    • 2013-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多