Can't endBackgroundTask: no background task exists with identifier *, or it may have already been ended

2.5k views Asked by At

My app monitors user location updates (not necessarily significant location change) with:

someLocationManager = [[CLLocationManager alloc] init];
[someLocationManager setDelegate:self];
[someLocationManager startUpdatingLocation];

When app (in simulator) is in foreground everything works ok, but when suspending it to background (not terminating), I get this error to the log:

Can't endBackgroundTask: no background task exists with identifier *, or it may have already been ended

All other answers for this error have no connection to location services, but only generic background task issues.

2

There are 2 answers

3
mllm On BEST ANSWER

So it appears that the following 3 settings should be made in order for background location update to work properly. Do this and fix the problem:

  1. Make NSLocationAlwaysUsageDescription key is set (with value as a reason for the permission) in info.plist.
  2. Make sure to call [someLocationManager requestAlwaysAuthorization]; when asking for permission.
  3. Enable Location Updates in Background Modes in Capabilities section in your target settings. Not sure about this one, but sounds right.

Enjoy!

0
Anand3777 On

Add the below code at the time of getting location authorization from the user. Because apple changed the default allowsBackgroundLocationUpdates NO from iOS9.

'if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9) {
    locationManager.allowsBackgroundLocationUpdates = YES;
}'