Objective-C Category for Location Authorization Request for iOS8 with Xcode 5 Support [Update]

<update date=″17.9.15″>

</update>

When you use the location of the user in your app and want also your iOS8 users to be able to enable using their location, you have to explicitly ask the user for it using this or this selector. There are two problems connected to that:

  1. You need lots of boilerplate code.
  2. If you need to provision your apps with Xcode 5, the application will not compile anymore using one of the new selectors which are only available in Xcode 6’s SDK.

So, to fix these two problems, I have come up with the following solution. I have created a category that extends UIViewController providing two convenience methods supporting authorization requests for the update of the user’s location. These two methods provide the boilerplate code for the authorization requests for ‘authorization always’ (so you can retrieve the user’s location when the app is as well running as in the background) and for ‘authorization when in use’ (you can retrieve the user’s location only when the app is in use). Furthermore, the calls to the selectors are wrapped in a way that you can use the category as well in Xcode 5 as in Xcode 6.

All you have to do is the following:

  • Copy both files to your project
  • Include #import “UIViewController+LocationAuthorization.h” in the file where you want to show the authorization request.
  • Put a message for ‘authorization always’ and/or for ‘authorization when in use’ in your app’s info plist directly under ‘Information Property List’. For always use the key NSLocationAlwaysUsageDescription and for when in use employ the key NSLocationWhenInUseUsageDescription. As type choose ‘String’. As value enter a message of your choice.
  • Call [self checkLocationAuthorizationAlwaysStatus] or [self checkLocationAuthorizationWhenInUseStatus] to show the authorization request (message will only shown the first time the code is executed.).

Download the category here.

Questions and comments are welcome below.

Posted in iOs, iPad, iPhone, Objective-C