【发布时间】: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