2
votes

I am not sure if this is a bug or some sort of configuration issue but(but i put every option in info.plist, locationManager.allowsBackgroundLocationUpdates = true and every other tip i found on the web and still the problem exists)

locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { }

stops updating the location after some (random) time when in background (the app closed). On a iphone 4s ios 8.4 this problem is not present.

Maybe some one else confrunted this problem? Thanks

3
Can you show us your code ?Chajmz
Did you solve your problem. I'm facing the same issue.Rahul Chaurasia
Me too. It seems to happen with no reason, just stop tracking also if you registered some regions in the background. Does anybody figured it out? Is it really a bug? With 9.3.1 it never happen to me...Antonio Romano

3 Answers

0
votes

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
0
votes

In addition you also can use the significant change location service, with this service, even when your is suspend, the iOS will wake your app

look for: startMonitoringSignificantLocationChanges in: https://developer.apple.com/library/mac/documentation/CoreLocation/Reference/CLLocationManager_Class/index.html#//apple_ref/occ/instm/CLLocationManager/stopMonitoringSignificantLocationChanges

0
votes

I have got the same problem. the way I have found to solve this problem is go to settings -> privacy -> location services -> find your app -> allow location access -> switch to never -> go back to location services -> then allow location access to switch on your service again. but only work sometimes.

also, as I know(may be I'm worry.) if the accuracy is not enough that what you have set. location update wouldn't be fired up.