位置更新在后台停止-iOS
我正在为iOS构建一个应用程序,它需要知道用户何时在距离他们家20-30米的范围内 当用户距离其家500米时,我使用geofence触发事件,然后使用CLLocationManager开始跟踪实际位置并检查到其家的距离 我的问题是,当我在geofence事件中调用“startUpdatingLocation”时,应用程序处于后台,它只运行20秒左右,然后停止任何回调。 如果我在20秒后打开应用程序并将其放回后台,那么它会一直给我回叫,直到我停止它。 如果我在应用程序运行时调用“startUpdatingLocation”,然后将其放在后台,则它会继续正常运行 我的初始化代码如下所示:位置更新在后台停止-iOS,ios,background,location,Ios,Background,Location,我正在为iOS构建一个应用程序,它需要知道用户何时在距离他们家20-30米的范围内 当用户距离其家500米时,我使用geofence触发事件,然后使用CLLocationManager开始跟踪实际位置并检查到其家的距离 我的问题是,当我在geofence事件中调用“startUpdatingLocation”时,应用程序处于后台,它只运行20秒左右,然后停止任何回调。 如果我在20秒后打开应用程序并将其放回后台,那么它会一直给我回叫,直到我停止它。 如果我在应用程序运行时调用“startUpda
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()
}
}
当我开始更新位置时,我会这样做:
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。
我真的不知道该怎么办,希望有人能帮忙
locationManager.startMonitoringSignificantLocationChanges()
用这行代替这行
locationManager.startUpdatingLocation()
它将解决您的问题或同时使用两者;您可以在前台启用重要的位置更改。这将允许您在地理围栏中时在后台调用
startUpdatingLocation
,并获得持续更新。非常感谢!这正是我需要的答案!现在运行得很好。