How to enable location services in iOS 8

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:

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.

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.

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:

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

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.