I solved it restarting the location manager every 3 minutes:
In your delegate method (locationDidUpdateToLocation
in my case) put something like
self.timeElapsedSinceLastTrackingUpdate = NSDate().timeIntervalSinceDate(self.lastUpdatedTime)
if timeElapsedSinceLastTrackingUpdate > 180 {
self.lastUpdatedTime = NSDate()
self.LocationMgr.stopUpdatingLocation()
UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler({})
NSTimer.scheduledTimerWithTimeInterval(180, target: self.LocationMgr, selector: #selector(self.LocationMgr.startUpdatingLocation), userInfo: nil, repeats: false)
}
Also Make sure you have both keys (NSLocationAlwaysUsageDescription
and NSLocationWhenInUseUsageDescription
) in your Info.plist
<key>NSLocationAlwaysUsageDescription</key>
<string>This app needs to use your location</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app needs to use your location</string>
And finally, be sure to set these variables on the location manager:
pausesLocationUpdatesAutomatically = false
and
allowsBackgroundLocationUpdates = true