【问题标题】:Location updates stops in background - iOS位置更新在后台停止 - iOS
【发布时间】:2017-12-16 17:44:50
【问题描述】:

我正在为 iOS 构建一个应用程序,它需要知道用户何时在他们家的 20-30 米范围内。

当用户离家 500m 时,我使用地理围栏触发事件,然后我使用 CLLocationManager 开始跟踪实际位置并检查到他们家的距离。

我的问题是,当我在应用程序处于后台时从我的地理围栏事件中调用“startUpdatingLocation”时,它只运行了大约 20 秒,然后停止提供任何回调。 如果我在 20 秒后打开应用程序并将其放回后台,那么它会一直给我回调,直到我停止它。 如果我在应用程序运行时调用“startUpdatingLocation”,然后将其置于后台,它会继续运行。

我的初始化代码如下所示:

init(config: AutounlockConfiguration) {
    super.init()

    self.config = config

    self.locationManager = CLLocationManager()

    self.locationManager.requestAlwaysAuthorization()
    self.locationManager.requestWhenInUseAuthorization()

    self.locationManager.delegate = self
    self.locationManager.pausesLocationUpdatesAutomatically = false
    self.locationManager.allowsBackgroundLocationUpdates = true
    self.locationManager.activityType = .automotiveNavigation
    self.locationManager.distanceFilter = kCLDistanceFilterNone
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
}

当我开始更新位置时,我会这样做:

func startFetchingUserLocation() {
    if (!isFetchingLocation) {
        isFetchingLocation = true
        DanaDebugger.presentDebugPushMessage(message: "fetching new location")
        locationManager.startUpdatingLocation()
    }
}

我已启用位置的后台模式,并在我的 info.plist 文件中包含 NSLocationAlwaysUsageDescription。 我真的不知道从哪里开始,希望有人能够提供帮助。

【问题讨论】:

    标签: ios background location


    【解决方案1】:
    locationManager.startMonitoringSignificantLocationChanges()
    

    用这一行代替这一行

    locationManager.startUpdatingLocation()
    

    它会解决你的问题

    【讨论】:

    • 或者两者都用;您可以在前台启用重大位置更改。然后,当您在地理围栏中时,这将允许您在后台调用 startUpdatingLocation 并获得持续更新。
    • 非常感谢!这正是我需要的答案!现在完美运行。
    猜你喜欢
    • 1970-01-01
    • 2017-03-26
    • 1970-01-01
    • 1970-01-01
    • 2016-09-30
    • 1970-01-01
    • 2019-02-03
    • 1970-01-01
    • 2015-03-11
    相关资源
    最近更新 更多