iosobjective-ccore-locationuibackgroundtask

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


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.


Solution

  • 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!