22

My app tracks location while in background... the location background mode is enabled and i use

[self.locationManager startUpdatingLocation];

This works great on iOS7-8 but it stopped working on iOS9

In simulator it works, but on real device i get no callback while in background... when running the same code on iOS8 device i get normal callbacks as before

Is this documented? Why does it work in simulator and not in device? is it a bug?

When using startSignificantChangeUpdates it works on iOS9, i wonder if this is some kind of battery saving feature, that they possibly prohibited startUpdatingLocation

Peter Lapisu
  • 19,915
  • 16
  • 123
  • 179
  • 1
    Please see the belo http://stackoverflow.com/questions/30808192/allowsbackgroundlocationupdates-in-cllocationmanager-in-ios9 – Dipen Sep 24 '15 at 09:50

4 Answers4

34

Use:

if ([self.locationManager respondsToSelector:@selector(setAllowsBackgroundLocationUpdates:)]) {
    [self.locationManager setAllowsBackgroundLocationUpdates:YES];
}

to avoid using a macro that evaluates the system version. This will call setAllowsBackgroundLocationUpdates: on iOS 9 but not on iOS 8.

sigsegfalt
  • 356
  • 2
  • 6
15

Please use below reference for location in ios 9

allowsBackgroundLocationUpdates in CLLocationManager in iOS9

This new property is explained in the WWDC session "What's New in Core Location".

The default value is NO if you link against iOS 9.

If your app uses location in the background (without showing the blue status bar) you have to set allowsBackgroundLocationUpdates to YES in addition to setting the background mode capability in Info.plist. Otherwise location updates are only delivered in foreground. The advantage is that you can now have location managers with background location updates and other location managers with only foreground location updates in the same app. You can also reset the value to NO to change the behavior.

The documentation is pretty clear about it:

By default, this is NO for applications linked against iOS 9.0 or later, regardless of minimum deployment target.

With UIBackgroundModes set to include "location" in Info.plist, you must also set this property to YES at runtime whenever calling -startUpdatingLocation with the intent to continue in the background.

Setting this property to YES when UIBackgroundModes does not include "location" is a fatal error.

Resetting this property to NO is equivalent to omitting "location" from the UIBackgroundModes value. Access to location is still permitted whenever the application is running (ie not suspended), and has sufficient authorization (ie it has WhenInUse authorization and is in use, or it has Always authorization). However, the app will still be subject to the usual task suspension rules.

See -requestWhenInUseAuthorization and -requestAlwaysAuthorization for more details on possible authorization values.

Thanks

Community
  • 1
  • 1
Dipen
  • 268
  • 1
  • 14
  • 2
    Please note that your program will crash if you include this property on system versions less than 9.0. You will see: [CLLocationManager setAllowsBackgroundLocationUpdates:]: unrecognized selector sent to instance. Please see my answer for managing this. – mkabatek Oct 15 '15 at 17:05
  • Xcode 7 changes this suggestion to: if #available(iOS 9.0, *) { self.locationManager.allowsBackgroundLocationUpdates = true } else { // Fallback on earlier versions } – Ben Carroll Jun 05 '16 at 21:41
7

I had the same issue, you need to add something like this:

if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"9.0")){

    [self.locationManager setAllowsBackgroundLocationUpdates:YES];

}

My macro for checking the system version looks like this:

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

The following forum post helped me:

https://forums.developer.apple.com/thread/6895

mkabatek
  • 1,244
  • 12
  • 23
2

The new method is locationManager -> requestLocation() see https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/ Also added allowsBackgroundLocationUpdates (only for iOS9).

ares777
  • 3,590
  • 1
  • 22
  • 23