Recently, a very common issue of location manager services was found in one of our iOS8 app. The core bug is that, although using regular working code for enabling location service, the snippet didn’t functioned. And to the worst, it happened only with the devices which have iOS 8 or higher versions. After spending hours of research, we came found that the bug was because of new iOS 8 and not because of the code.

 

Regular Case

In general case,  if we need to use location manager into our app we uses following code.

In .h file set CLLocationManagerDelegate protocol.
In .m file in viewDidLoad method:

// Create & allocate Location Manager
CLLocationManager  *locationManager  = [[CLLocationManager alloc] init];
// Set delegate
locationManager.delegate  = self;
// Start Location Manager[locationManager StartUpdatingLocations];

// Location Manager Delegate Method
-(void)locationManager:(CLLocationManager*)manager didUpdateLocations:(NSArray*)locations{
NSLog(@”Location Updated”);
}

This is the general code used by iOS developers to enable location manager services. When the application is built with this code,  it will prompt the user to allow enable location services for the  first time. On pressing allow, it will start updating user location & track current location.

This worked fine upto iOS 7.1.2. But when iOS 8 was launched, an app stopped giving the alert for enabling location manager service, though using the same code.  Additionally, an app didn’t give   any error or crash issue, by which  we can track some idea about how to deal with it.

Now in this scenario, it becomes necessary to update the apps that are purely based on Location to make sure it continues working on devices with higher iOS version as well.

Another confusion is caused , when testing is done on simulator as, it doesn’t give any idea about this bug. So how to solve this issue?

 

Here’s the solution for iOS8

 

If you are working on core location and your app supports iOS 8 than you need to put some extra efforts. Just few more steps & your application will be back on track & start working normally as it did earlier.

  1. Simply add key to your Info.plist & request authorization from location manager asking it to start. You need to add 2 keys for new location authorization.
  2. Make sure you need to add one or both the keys.

(Note: If you forget to add one or both of the keys, you can call startUpdatingLocation but location manager will not actually start. It will not send failure message to the delegate. Because its never started still, it can’t be failed.)

This 2 strings you need to add into Info.plist

  • NSLocationWhenInUseUsageDescription
  • NSLocationAlwaysUsageDescription

 

See image below:

image1

These key strings describes the requirement to find the current location.
Next step you need to do is to use one of these line:

[locationManager requestWhenInUseAuthorization];  or
[locationManager requestAlwaysAuthorization];

On using the first line, it will only ask for authorisation at once. In case when second line is used, authorisation alert will appear only when this method is called.

It is purely dependent on your application requirement that which you need to implement. Also make sure this line needs to implemented only in iOS 8 or higher. So, for the easy work just put the  validation regarding iOS check.

On implementing in this way, you can make your code compatible for different iOS environment. Make sure function works same in both the environment.

 

Below is the full code with implementation in iOS 8:

 

in .h file:

define  CLLocationManagerDelegate protocol

in .m file:

-(void)viewDidLoad{[super viewDidLoad];
// Create & allocate Location Manager
CLLocationManager  *locationManager  = [[CLLocationManager alloc] init];
// Set delegate
locationManager.delegate  = self;

if ([[UIDevice currentDevice] systemVersion] >= 8.0){
if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]){[locationManager requestWhenInUseAuthorization];
}
}
// Start Location Manager[locationManager StartUpdatingLocations];
}

// Location Manager Delegate Method
-(void)locationManager:(CLLocationManager*)manager didUpdateLocations:(NSArray*)locations{
NSLog(@”Location Updated”);
}

If you are looking to build your iOS 8 apps or looking to fix any issue with your current apps , you can Contact Us on info@cyberlobe.com or fill our form here.

(Visited 10 times, 1 visits today)

Tags: Enable location services ios 8, How to enable location services ios 8, ios 8 location tracking, iphone enable location services ios 8, Location services error iOS 8, Location services error iphone
Share
Share