I'm trying to use MapKit on iOS 8 and I keep getting the error:
Trying to start MapKit location updates without prompting for location authorization. Must call
-[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager
requestAlwaysAuthorization] first.
Looking it up here, I found that I had to implement NSLocationWhenInUsageDescription in my plist but nothing happens and I still get that error in the console. What am I doing wrong?
1.- Add the following lines to your info.plist
<key>NSLocationWhenInUseUsageDescription</key>
<string>The spirit of stack overflow is coders helping coders</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>I have learned more on stack overflow than anything else</string>
2.- Prompt for location authorization:
if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[self.locationManager requestAlwaysAuthorization];
3.-Update location when user have accepted...
- (void) locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
if (status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse) {
[self.locationManager startUpdatingLocation];
}
}
just add those code in CLLocationManager
initialization.
for example: @property (strong, nonatomic) CLLocationManager *locationManager;
then in your implementation just call [self.locationManager requestWhenInUseAuthorization];
before calling the update function.
after your cllocationmanager
initilaisation check for ios 8 and ask for the permission
locationManager = [[CLLocationManager alloc] init];// [[[CLLocationManager alloc] init] autorelease];
locationManager.delegate = self;
// Check for iOS 8. Without this guard the code will crash with "unknown selector" on iOS 7.
if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[locationManager requestWhenInUseAuthorization];
}
reference http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/
In addition to implementing the keys in PList (I have NSLocationWhenInUseUsageDescritpion
and Privacy - Location Usage Description
), I solved the warning with the following code:
if ([CLLocationManager locationServicesEnabled])
{
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
if (status == kCLAuthorizationStatusRestricted || status == kCLAuthorizationStatusDenied || status == kCLAuthorizationStatusNotDetermined) {
} else {
_mapView.showsUserLocation = YES;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.