位置更新在后台停止 - iOS

时间:2017-07-12 10:34:03

标签: ios background location

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

用户地理围栏在用户离家500米时触发事件,然后我使用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()
    }
}

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

1 个答案:

答案 0 :(得分:2)

locationManager.startMonitoringSignificantLocationChanges()

使用此行代替此行

locationManager.startUpdatingLocation()

它将解决您的问题

相关问题