John Drax
John Drax

Reputation: 123

ios(swift) location in background (ios 9.3.2) stops working

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

Upvotes: 2

Views: 1022

Answers (3)

Carl Hung
Carl Hung

Reputation: 555

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.

Upvotes: 0

vova sarkisyan
vova sarkisyan

Reputation: 56

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

Upvotes: 0

danywarner
danywarner

Reputation: 958

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

Upvotes: 0

Related Questions