Background Location Services not working in iOS 7

I’ve recently upgraded my iOS devices to use iOS 7. One of the apps that we’re developing uses background location services to track device location and all of our testers have reported that the app no longer appears to track in the background under iOS 7.

We have verified that backgrounding for the app is enabled in the settings on the device and the previous build worked flawlessly under iOS 6. Even if the device were cycled, the app would restart after a location update.

  • CoreLocation kCLErrorDomain error 5
  • CLLocationManager responsiveness
  • CLLocationManager geo-fencing/startMonitoringForRegion: vs. startMonitoringForSignificantLocationChanges: vs. 10-minute startUpdating calls
  • how to return multiple values from a method
  • iOS re-check location on load from background
  • How can I prompt the user to turn on location services after user has denied their use
  • Is there something else that needs to be done to make this work under iOS 7?

    6 Solutions Collect From Internet About “Background Location Services not working in iOS 7”

    I apologise for delay in posting the solution. I have been busy in the last few months and got caught up with my personal life until I forgot about this thread. Here is the solution that I used to get continuous location from iOS 7 devices no matter it is in foreground or background.

    You may find the full solution and explanation from blog and also github:-

    1. Background Location Update Programming for iOS 7 and 8

    2. Github Project: Background Location Update Programming for iOS 7 and 8

    Methods and Explanation:-

    1. I restart the location manager every 1 minute in function didUpdateLocations

    2. I allow the location manager to get the locations from the device for 10 seconds before shut it down (to save battery).

    Partial Code Below:-

    -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
    
    for(int i=0;i<locations.count;i++){
        CLLocation * newLocation = [locations objectAtIndex:i];
        CLLocationCoordinate2D theLocation = newLocation.coordinate;
        CLLocationAccuracy theAccuracy = newLocation.horizontalAccuracy;
        NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
    
        if (locationAge > 30.0)
            continue;
    
        //Select only valid location and also location with good accuracy
        if(newLocation!=nil&&theAccuracy>0
           &&theAccuracy<2000
           &&(!(theLocation.latitude==0.0&&theLocation.longitude==0.0))){
            self.myLastLocation = theLocation;
            self.myLastLocationAccuracy= theAccuracy;
            NSMutableDictionary * dict = [[NSMutableDictionary alloc]init];
            [dict setObject:[NSNumber numberWithFloat:theLocation.latitude] forKey:@"latitude"];
            [dict setObject:[NSNumber numberWithFloat:theLocation.longitude] forKey:@"longitude"];
            [dict setObject:[NSNumber numberWithFloat:theAccuracy] forKey:@"theAccuracy"];
            //Add the vallid location with good accuracy into an array
            //Every 1 minute, I will select the best location based on accuracy and send to server
            [self.shareModel.myLocationArray addObject:dict];
        }
    }
    
    //If the timer still valid, return it (Will not run the code below)
    if (self.shareModel.timer)
        return;
    
    self.shareModel.bgTask = [BackgroundTaskManager sharedBackgroundTaskManager];
    [self.shareModel.bgTask beginNewBackgroundTask];
    
    //Restart the locationMaanger after 1 minute
    self.shareModel.timer = [NSTimer scheduledTimerWithTimeInterval:60 target:self
                                                           selector:@selector(restartLocationUpdates)
                                                           userInfo:nil
                                                            repeats:NO];
    
    //Will only stop the locationManager after 10 seconds, so that we can get some accurate locations
    //The location manager will only operate for 10 seconds to save battery
    NSTimer * delay10Seconds;
    delay10Seconds = [NSTimer scheduledTimerWithTimeInterval:10 target:self
                                                    selector:@selector(stopLocationDelayBy10Seconds)
                                                    userInfo:nil
                                                     repeats:NO];
     }
    

    Update on May 2014: I got a few requests for adding sample codes on sending the location to server for a certain time interval. I have added the sample codes and also combined a fix for BackgroundTaskManager to solve a glitch for background running over an extended period of time. If you have any questions, you are welcomed to join us for a discussion here: Background Location Update Programming for iOS 7 with Location Update to Server Discussion

    Update on January 2015: If you want to get the location update even when the app is suspended, please see: How to Get Location Updates for iOS 7 and 8 Even when the App is Suspended

    If you look at WWDC 2013 Session video #204 – What’s new with multitasking pdf, page number 15 clearly mentions that apps wont launch in the background if user kills it from the app switcher. Please see the image,

    enter image description here

    I think they made an optimization (probably using motion sensors), to detect “relatively” stationary positioning of the phone and they stop the location updates. This is only a speculation, but my tests currently show:

    1. Starting location updates; (tested with accuracy of 10 and 100 meters, 3 times each)
    2. Turn device’s screen off to put the app in the background;
    3. Leave the device stationary (e.g. on a desk) for 30 min.

    The data I log shows the geo-updates stop coming after ~ 15m and 30s. With that all other background processing you do is also terminated.

    My device is iPhone 5 with iOS 7.

    I am 95% sure, this wasn’t the case on iOS 6/6.1. Where getting geo updates with 100m accuracy used to give you pretty much continuous running in background.

    Update

    If you restart the location manager every 8 minutes, it should run continuously.

    Update #2

    I haven’t tested this in latest, but this is how I restarted it when I wrote the post. I hope this is helpful.

    - (void)tryRestartLocationManager
    {
        NSTimeInterval now = [[NSDate date] timeIntervalSince1970];
    
        int seconds = round(floor(now - locationManagerStartTimestamp));
    
        if ( seconds > (60 * 8) ) {
            [locationManager stopUpdatingLocation];
            [locationManager startUpdatingLocation];
            locationManagerStartTimestamp = now;
        }
    }
    

    One of my iOS app needs to send location update to server at regular intervals and following approach worked for us….

    Starting in iOS 7:

    • an app must have already been using location services
      (startUpdatingLocation) BEFORE having been backgrounded in order for
      the eligible for background run time
    • the background grace period timeout was reduced from 10 minutes to 3
      minutes
    • stopping location updates (via stopUpdatingLocation) will start the 3
      minute backgroundTimeRemaining count down
    • starting location updates while already in the background will not
      reset the backgroundTimeRemaining

    So, DO NOT STOP the location updates anytime…Instead, prefer to use the necessary accuracy (fine location vs coarse location). Coarse location does not consume much battery…so this solution solves your problem.

    After much searching online, found a link which provide a viable solution for iOS 7. The solution is as follows:

    • While the app is still in the foreground, start location updates
      (startUpdatingLocation) but set the accuracy and distance filters to
      very course-grained (e.g. 3 km) updates. It is important to do this
      in the foreground (in applicationDidEnterBackground is too late).
    • When a fine-grained resolution is required, temporarily set the
      accuracy and distance filters to get the best possible location, and
      then revert them back to the course-grained values – but never stop
      location updates.
    • Because location updates are always enabled, the app will not get
      suspended when it goes to the background.

    And make sure you add the following to your application’s Info.plist
    “location” to the UIBackgroundModes key
    “location-services” and “gps” to the UIRequiredDeviceCapabilities key

    Credits: http://gooddevbaddev.wordpress.com/2013/10/22/ios-7-running-location-based-apps-in-the-background/

    I found another thread Start Location Manager in iOS 7 from background task Sash mentioned that

    I found the problem/solution. When it is time to start location service and stop background task, background task should be stopped with a delay (I set 1 second). Otherwise location service wont start.

    Can anyone try that and verify?

    Nikolay, can you paste your code here? I tried to restart the location manager every 8 minutes but it does not run continuously.

    Update:

    After searching High and Low, I found the Solution from Apple Forum!

    In iOS 7, you can not start the location service in background. If you want the location service to keep running in the background, you have to start it in foreground and it will continue to run in the background.

    If you were like me, stop the location service and use timer to re-start it in the background, it will NOT work.

    For more detailed information, you can watch the first 8 minutes of video 307 from WWDC 2013: https://developer.apple.com/wwdc/videos/

    Feb 2014 Update:
    I can get the location continuously from device using iOS 7 after several months of trying. You may see the full answer here: Background Location Services not working in iOS 7

    Is the icon on the status bar turned on? It’s a strange behaviour I had too.
    Check my question: startMonitoringSignificantLocationChanges but after some time didUpdateLocations is not called anymore

    I discovered that the significant location changes was on but simply stopping and restarting the service (for significant changes) was not firing new locations.