【问题标题】:CLLocationManager didUpdateLocations is just called twice in background modeCLLocationManager didUpdateLocations 在后台模式下只被调用了两次
【发布时间】:2015-11-28 23:28:30
【问题描述】:

我想使用 CLLocationManager 在前台和后台更新用户当前的 GPS 位置。 我在 info.plist 和目标功能下注册了使用位置背景模式的应用程序。 在 iPhone 模拟器上一切正常,但在我真正的 iPhone 上,当应用程序处于后台时,函数 didUpdateLocations 只会被调用两次。 我使用 gpx 文件在 Xcode 中模拟 GPS 数据。它在应用程序处于前台时工作,我可以在 MapView 上看到进度。

这是我的 ViewController 中的代码:

lazy var locationManager: CLLocationManager! = {
    let manager = CLLocationManager()
    manager.desiredAccuracy = kCLLocationAccuracyBest
    manager.delegate = self
    manager.requestAlwaysAuthorization()
    manager.pausesLocationUpdatesAutomatically = false
    //manager.distanceFilter = 250
    return manager
}()



//Location manager delegate function

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    if(UIApplication.sharedApplication().applicationState == .Background){
        print("background call")
    }else{
        print("foreground call")
    }

    // Save GPS coordinates
    saveGpsCoordinates(locations[0])
}

我正在使用: iPhone 5 和 Xcode 7.0 beta 5 上的 IOS 9

有人知道为什么我的真实设备上没有收到任何后台代理调用吗?

谢谢。

【问题讨论】:

    标签: swift cllocationmanager ios9 xcode7-beta5


    【解决方案1】:

    你应该在 info.plist 中添加 NSLocationAlwaysUsageDescription。祝你好运!

    【讨论】:

      【解决方案2】:

      我在迁移到 iOS9 后遇到了类似的问题。

      如以下问题的答案所述,请确保您将allowBackgroundLocationUpdates 设置为YES

      allowsBackgroundLocationUpdates in CLLocationManager in iOS9

      【讨论】:

      • 这应该是公认的答案。无论您在 plist 中指定什么,只要您不在实际的 CLLocationManager 对象上设置此属性,您就不会获得后台更新。
      【解决方案3】:

      确保将 pausesLocationUpdatesAutomatically 设置为 TRUE。因为当它设置为 FALSE 时,您会获得更多位置更新,即使是看起来相同的位置。

      TRUE 也是默认值。

      【讨论】:

        【解决方案4】:

        两件事:

        1. 我在您的代码中没有看到对 startUpdatingLocation 的调用。

        2. (假设 1 是正确的)您说您正在使用 GPX 文件来模拟位置,如果您使用 GPX 文件来模拟位置而不是使用 locationManager 来获取位置并且因为您这样做了不调用startUpdatingLocation,locationManager实际上并没有在前台或后台运行。

        【讨论】:

        • 我从 UISwitch 的 IBAction 启动 locationmanager,它在模拟器(背景和前景)中运行良好。
        猜你喜欢
        • 1970-01-01
        • 2016-05-29
        • 1970-01-01
        • 2016-07-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多