Reputation: 33
I am building an app for iOS that needs to know when the user is in a range of 20-30 meters of their home.
I user geofence to trigger an event when the user is 500m from their home, then I use CLLocationManager to start tracking the actual location and check distance to their home.
My problem is that when I call "startUpdatingLocation" from my geofence event while the app is in the background, it only runs for like 20 seconds and then stops giving any callbacks. If I, after the 20 seconds have passed open the app and puts it back to the background then it keeps giving me callbacks until I stop it. If I call "startUpdatingLocation" while the app is running and then puts it into the background, it keeps running as it should.
My code for initialisation looks like this:
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
}
and when I start updating location I do it like this:
func startFetchingUserLocation() {
if (!isFetchingLocation) {
isFetchingLocation = true
DanaDebugger.presentDebugPushMessage(message: "fetching new location")
locationManager.startUpdatingLocation()
}
}
I have enabled the background mode for location, and included the NSLocationAlwaysUsageDescription in my info.plist file. I really have no clue where to go from here, hope somebody is able to help.
Upvotes: 2
Views: 425
Reputation: 745
locationManager.startMonitoringSignificantLocationChanges()
Use this line instead of this line
locationManager.startUpdatingLocation()
it will solve your problem
Upvotes: 3