New “When In Use” Location Tracking in iOS 8

Posted by on Sep 12, 2014 in code, iOS | No Comments

Location AlertIf your app tracks the user’s location, you may notice when you run it in iOS8, the location tracking alert asks for permission to “access your location even when you are not using the app“. If your app only accesses the user’s location when the app is running, you can present a less scary message by requesting “when in use” authorization only.

The solution for iOS 8 is to call requestWhenInUseAuthorization on your CLLocationManager instance.



CLLocationManager locationManager = [[CLLocationManager alloc] init];

if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
    [locationManager requestWhenInUseAuthorization];
}

With this change, you’ll see the friendlier message that the app wants your location “while you use the app”.

One caveat: if you’re still building with Xcode 5 and the iOS7 SDK, the above code won’t compile, because the selector 
requestWhenInUseAuthorization doesn’t exist. The solution is to call performSelector instead:

if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
    [locationManager performSelector:@selector(requestWhenInUseAuthorization)];
}

For more detail on the new iOS8 Location Services options, see Nick Arnott’s detailed writeup.

Leave a Reply