Swift app crashes with `unrecognized selector sent to instance` on CLLocationManager
As requestWhenInUseAuthorization
method is only available on iOS 8 (according to Apple Documentation) you have to wrap it:
Swift code:
if (locationmgr.respondsToSelector(Selector("requestWhenInUseAuthorization"))) { locationmgr.requestWhenInUseAuthorization()}
And the same for Objective-C:
if ([locationmgr respondsToSelector:@selector(requestWhenInUseAuthorization)]) { [locationmgr requestWhenInUseAuthorization];}
This code won't run on iOS 7 and below - according the docs, requestWhenInUseAuthorization
is only available on iOS 8:
Availability:
Available in iOS 8.0 and later.
Here is what I'm using to support the new core location method in iOS 7 with no errors:
SEL requestSelector = NSSelectorFromString(@"requestWhenInUseAuthorization");if ([locationManager respondsToSelector:requestSelector]) { [locationManager performSelector:requestSelector withObject:NULL];}