Wednesday, October 8, 2014

How to update location in iOS 8?

Problem- 

In iOS 8 you will face the issue that location manager delegates unable to get callback when the location is updating if you are using the given code snippet for getting location updates.

  _locationManager = [[CLLocationManager alloc] init];
  
  _locationManager.delegate = self;
  _locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
  
  [_locationManager startUpdatingLocation]; 


Solution-

In iOS 8 you have to call requestWhenInUseAuthorization or requestAlwaysAuthorization method before calling startUpdatingLocation methods.

  _locationManager = [[CLLocationManager alloc] init];
  
  _locationManager.delegate = self;
  _locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
  
  if(IS_OS_8_OR_LATER)
   [_locationManager requestWhenInUseAuthorization];
  
  [_locationManager startUpdatingLocation]; 

Also, you have to add NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription key in the plist.

If you did’t specify the key into the plist then requestAlwaysAuthorization method will do nothing, as your app should be assumed not to support Always authorization. 

If you always wants to use the locations update even when the app is in background then you have to call requestAlwaysAuthorization & specify NSLocationAlwaysUsageDescription key in the plist. 

In other cases where you don’t want to call location method update always, you can use requestWhenInUseAuthorization & specify NSLocationWhenInUseUsageDescription key in the plist.


No comments:

Post a Comment