When CLLocationManager doesn’t play nice.

Here’s a change in pace, an article that’s not about Android or Java. I’m way stronger with Android and Java but sometimes I do find myself developing with Objective C. Today I’m going to briefly discuss something annoying about CLLocationManager and the CoreLocation framework, or why it might not be working in your(read my) application. Keep reading for more details.

So you add the CoreLocation framework to your project in your Build Settings. You have a CLLocationManager object created, you’ve set the desired accuracy and you’ve hooked in the delegate. You have your didFailWithError and your didUpdateLocation methods implemented, and you’ve made a call to CLLocationManager’s startUpdatingLocation method. But wait, nothing is happening? An error isn’t even being thrown in the didFailWithError method. What the fuck? “Am I missing something here?”, well yeah you are but Xcode isn’t going to give you any hints. I was pretty surprised by this. Usually when something like this happens with Android you get some sort of hint or information. Xcode? It’s completely silent.

So how do you fix this?

It’s actually pretty simple. First you wanna make a call to requestAlwaysAuthorization or requestWhenInUseAuthorization before you call startUpdatingLocation. Here’s a quick example:

And then in your Info.plist add NSLocationAlwaysUsageDescription and NSLocationWhenInUseUsageDescription. I assume using one or the other will also working depending what method you are calling before startUpdatingLocation. Add them as strings, and then set the string to whatever you want to be displayed when the user is asked to give permission to share their location. For example, “your location is required to show your current position”.

And that’s it. When you run your project now you should get a popup on the device asking for permission. If you accept it the didUpdateLocation method will get a call. If the user rejects a call will be made to didFailWithError.

One thought on “When CLLocationManager doesn’t play nice.

  1. I can read the English, but I don’t understand what you wrote. lol. I’m sure it means something to other people, but the significance of what you wrote is totally beyond me.
    It reminds me of my university days when I was trying to read my textbooks.

    how have you been?

Leave a Reply

Your email address will not be published. Required fields are marked *