Reputation: 1901
I'm working on a navigation application, everything working in terminated, background and fore ground state. But in one scenario of terminated state startMonitoringSignificantLocationChanges is not handling itself.
The issue is
The code is straight
significantLocationManager = CLLocationManager()
significantLocationManager?.allowsBackgroundLocationUpdates = true
significantLocationManager?.pausesLocationUpdatesAutomatically = false
significantLocationManager?.requestAlwaysAuthorization()
and call the tracking when user needs by
significantLocationManager?.startMonitoringSignificantLocationChanges()
Rest I have handled the incoming location event in the app delegate to save in db.
So question is how should I handle this scenario in which straight line is drawn ?
Upvotes: 3
Views: 1297
Reputation: 1526
You can use Location update in background mode. From Apple documentation:
When you start the significant-change location service, a recently cached value may be reported to your delegate immediately. As new location data is obtained, the location manager calls your delegate's locationManager(_:didUpdateLocations:) method with the updated values. The locations parameter always contains at least one location and may contain more than one. Locations are always reported in the order in which they were determined, so the most recent location is always the last item in the array, as shown in Listing 2.
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let lastLocation = locations.last!
// Do something with the location.
}
Here you will get the last cached location in your device, and it should be very precise if you have location service turned on in your device of course.
Another thing to know is this. Note form Apple:
The significant-change location service requires authorization. For more information Requesting Authorization for Location Services.
Upvotes: 1
Reputation: 1417
From Apple documentation:
Apps can expect a notification as soon as the device moves 500 meters or more from its previous notification. It should not expect notifications more frequently than once every five minutes. If the device is able to retrieve data from the network, the location manager is much more likely to deliver notifications in a timely manner.
If you need to receive location updates as soon as possible I'd recommend to use startUpdatingLocation()
with desired distanceFilter
of CLLocationManager
.
Upvotes: 3