Continuous location update background iOS13

I am currently testing the background location mode in iOS 13, as I want to track the user´s location and motion (using the CMMotionManager) in the background. Therefore, i have my own (singleton) class handling the location tracking. I initialize the CLLocationManager in the following way:

  
 
func initializeLocationManager() -> CLLocationManager { 
let manager = locationManager ?? CLLocationManager() 
manager.delegate = self manager.requestAlwaysAuthorization() 
manager.allowsBackgroundLocationUpdates = true 
manager.pausesLocationUpdatesAutomatically = false 
manager.distanceFilter = kCLDistanceFilterNone 
manager.desiredAccuracy = kCLLocationAccuracyBest 
manager.activityType = .other 
return manager 
}

Then I start the following services:

  
func startLocationServices() { 
... 
locationManager.startUpdatingLocation() 
locationManager.startMonitoringVisits() 
locationManager.startMonitoringSignificantLocationChanges() 
... 
}

In addition, I implemented the CLLocationManagerDelegate-methods didChangeAuthorization(), didUpdateLocation().

In the .plist-file I appended the following entries:<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>

<string>...</string> 
<key>NSLocationWhenInUseUsageDescription</key> 
<string>...</string> 
<key>UIBackgroundModes</key> 
<array> 
<string>location</string> 
</array>

In my view controller, I call the startLocationServices. Currently, I set the app's authorization to track location data to ".authorizedAlways"

The location updates stop after approximately 60 - 130 minutes.


To solve the problem, I already added the didFinishLaunchingWithOptions-function, which triggers the location-updates again:nc application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

if let launchOptions = launchOptions, 
let isLocationKey = launchOptions[UIApplication.LaunchOptionsKey.location] as? Bool, 
isLocationKey { 
restartServices()  
} 
return true 
}

When the app gets awoken using this function, I managed to get continuous data on some tests, but sometimes the app was suspended again after a few minutes.

Last, I also tried a timer that restarts the location tracking every 5 minutes, but this does not seem to affect the update-duration at all.

I tested the application on iOS 12 and it gets continuous updates in 5/5 tests. So I guess the problem is related to iOS 13.


So my question is if there is a way of continuously receiving the location updates in the background in iOS13, or is there some option I am missing?

Thanks in advance.

Replies

Hey! I got exactly same problem in our app. It typically stopped running in the background not longer than 30 seconds in iOS 13, whereas in iOS 12, it works pretty well in most cases. It seems there’s a bug in the system? Have you figured out any solution?

The same issue happens even though we have switched to Always manually in the settings.

Hey! I didnt figure a solution yet. I get around 40 minutes until the app gets suspended. When the "significantLocationChanges" triggers the location updates again, I get just a few minutes/seconds.

We've started seeing issues since upgrading to iOS 13.3, only on certain devices where location manager doesn't return locations consistently (background or foreground) and will send a number of locations with (0,0) with 2500m accuracy. This was fine before 13.3. Various devices and carriers are exhibiting this behavior.


Running an app like Strava on the same phone at the same time, Strava gets locations, our app still doesn't. It is very strange -- made more difficult because it can't be debugging while connected to Xcode in any manner

Having exactly the same behaviour as mentioned starting with iOS 13.3

all good, update get in as expected - then

SceneDelegate sceneDidEnterBackground

<+52.53865689,+13.42571669> +/- 65.00m (speed -1.00 mps / course -1.00) @ 21.01.20, 23:39:07 Central European Standard Time
<+52.53862094,+13.42573375> +/- 65.00m (speed -1.00 mps / course -1.00) @ 21.01.20, 23:39:07 Central European Standard Time
<+52.53860520,+13.42569246> +/- 65.00m (speed -1.00 mps / course -1.00) @ 21.01.20, 23:39:09 Central European Standard Time
<+52.53878907,+13.42541768> +/- 65.00m (speed -1.00 mps / course -1.00) @ 21.01.20, 23:39:15 Central European Standard Time
<+52.52125886,+13.40727788> +/- 2500.00m (speed 0.00 mps / course 0.00) @ 21.01.20, 23:29:42 Central European Standard Time

No didUpdateLocations-events following after this 2500 m accuracy location.


Is there any workaround known?



I'm affected by the same issue: only on certain devices and only since iOS 13.3. Currently only on iPads.


My deployment target is iOS 12.4. What deployment target are you using? Probably that's what's different from those apps that are not affected?

@mattl proposed a workaround on his open StackOverflow ticket: https://stackoverflow.com/q/58298439/5922449


quoting his solution:

"I could drastically increase the duration before the application gets moved to the suspended state, by reducing the resources consumed in the background. -> I removed the motion collection completely from the app. -> When shifting into background I decrease the accuracy and increase the distanceFilter for locations to get less events, I stop some tasks that are updating the UI, I downsample the collection of other data sources, I do no exports of my data in the background or other workload. So now i get around 3000 minutes in the background."



@mattl, how much did u decrease your distance filter and accuracy parameters? and how much is your background trip path detection affected by those changes?