Solution: Location Services Not Working In iOS 8 Xcode 6

I am working on a App since past few months which uses location data. Things were working fine earlier. But after updating to the Xcode 6 and using the new iOS 8 simulator, I observed the location services have stopped working. After a bit of research I find the solution. If you are facing similar issue. Here is the fix for you –

Unlike iOS 7 where it automatically asks the user for permission to use location data, In iOS 8 we need to call a specific method before calling the startUpdatingLocation.

Which method to call depends upon whether you want to use location service only in foreground, i.e. while the app is running, or in the background as well, i.e. which is app is in background.

For the prior, we need to call – requestWhenInUseAuthorization
and for the later, we need to call – requestAlwaysAuthorization

Note:

These are iOS 8 specific methods. If your app supports iOS 7 or below, you also need to add the following code with the method call –

1
2
3
//Put the required method name inside the @selector.
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
}

So, the complete code will look like –

1
2
3
4
//Replace the methods below with your required method name.
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[self.locationManager requestWhenInUseAuthorization];
}
Note: You need to put this code before startUpdatingLocation method call.

Now we need to do one final thing, which is a requirement. We need to add a key to the Info.plist file. Depending on the method we are using above, the two keys are – NSLocationAlwaysUsageDescription and NSLocationWhenInUseUsageDescription.

These are string keys, where you will put the description or the message, which will appear while the system asks the user for location permission.

Location services should once again work fine now.

Let me know if you have any queries.

Namit Gupta

iOS Developer @GetVokal. Passionate about Mobile Apps. Loves to Design. You can reach him on his social profiles below.

Leave a Reply