1

I'm able to receive location updates on all (simulators and iPad device) but my iPhone device.

self.locationManager = [[[CLLocationManager alloc] init] autorelease];
self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
self.locationManager.pausesLocationUpdatesAutomatically = NO;
self.locationManager.delegate = self;

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *newLocation = locations.lastObject;
    NSLog(@"didUpdateLocations newLocation = %@", newLocation);
.
.
}

I receive didUpdateLocations (NSLog message) as long as app is in foreground but after I press Home button (i.e. app goes in background), I stop receiving notifications in didUpdateLocations. The 'console' doesn't show the NSLog message and thereafter even the location icon in status bar goes away within 10 seconds.

What works an what doesn't -

  1. Appears that iOS 6 installed on my other iPhone 3gs works fine. The little location icon doesn't go away.

  2. Test on iPad device with iOS 7 and the app keeps running in background.

  3. All simulators (iPhone and iPad) with iOS 6 as well as iOS 7 works as expected.

  4. When i test it on my iPhone 4 with iOS 7, the app doesn't run in background. So probably, this issue is only on iOS 7 (and even some specific iPhone devices).

Ashok
  • 6,224
  • 2
  • 37
  • 55

2 Answers2

3

I think I found the cause of this problem. The "Settings" app in iOS 7 has General > "Background App Refresh" which was turned OFF :( As soon as I turned it "ON", my app started running in background :)

Ashok
  • 6,224
  • 2
  • 37
  • 55
  • 1
    Make sure you handle if users have that off gracefully because I turned that setting off due to battery drain – RyanG Oct 07 '13 at 18:37
  • Thanks for that feedback. Just now, I was discussing this with my colleague that we need to provide appropriate feedback to the user and avoid unnecessary help desk calls. Do you know how to get this flag "Background App Refresh" value programmatically inside app? – Ashok Oct 07 '13 at 18:44
  • 1
    This may help (I did not test though): http://stackoverflow.com/a/19138393/480415 – RyanG Oct 07 '13 at 19:43
  • Even I stumbled upon the SAME post after I dropped the above note. `UIApplication.sharedApplication.backgroundRefreshStatus` works as defined. I verified. !! Thank you. – Ashok Oct 07 '13 at 21:00
  • I don't understand. This should only have an affect on significant location tracking/visits monitoring or geofence. It shouldn't affect normal location tracking... – mfaani Oct 01 '18 at 05:57
0

The above solution did not work for my particular case in iOS 8.X, therefore, I would like to post the solution that worked for me in hopes that it might help others.

I successfully setup silent Push Notifications which I was receiving in the Foreground, Background, and Suspended states. Once I received the PN I would need to obtain the location updates. I was able to do so while the application was in the Foreground, however, I was not able to do so while the application was in the background.

What I needed to do was add a key to the info.plist file called NSLocationAlwaysUsageDescription and give it a value. The value can be something like "Your location is needed in the background."

After adding the key I then added this code within my AppDelegate.m's application:didFinishLaunchingWithOptions: method -

// Check for iOS 8.X. Without this guard the code will crash with "unknown selector" on iOS 7.
if([[self locationManager] respondsToSelector:@selector(requestAlwaysAuthorization)])
{
    [[self locationManager] requestAlwaysAuthorization];
}

Once I added these two pieces I was able to successfully obtain location updates in the background.

Thanks, audible

Audible
  • 235
  • 3
  • 10