Friday, October 3, 2014

Getting User Location to work in iOS8

There were some minor changes to how iOS8 works with core location. Previously when requesting the user's location on the MapView with ShowsUserLocation a nice pop-up would automagically show up asking the user if this was ok. Apple decided to make this a bit more flexible, allowing the developers to enter their own message. So if you compile your pre-iOS 8 app with the new SDK this might break, since you now have to manually call a method to get that pop-up to show.

So the quick fix is to do the following:

1. In your info.plist file add one of the following key/value pairs:

2. I then have a Extensions.cs where I keep my various extensions, for this case I've just made a couple of checks to whether the selectors are available on the platform, so the code won't crash on previous versions of iOS.

3. Remove your previous call to mapView.ShowsUserLocation and do the following instead:

Now you respond to the AuthorizationChanged Event and set the ShosUserLocation flag accordingly.

Edit (3/10/2014 17:00):
Now that I have had the chance to play a bit more with it, I have a couple of observations that might help you. First don't let go of the CLLocationManager instance, i.e. have it as a class field otherwise the dialog will disappear pretty quickly and it won't let you get location updates.

I didn't mention that you will have to do some checks on what iOS version you run at now and only call this code when on iOS 8 and higher, to do this you can do something as follows.