- [CLLocationManager requestWhenInUseAuthorization]或 - [CLLocationManager requestAlwaysAuthorization]错误

Max*_*980 12 iphone core-location mapkit xcode6 ios8

这是我的代码,显示地图上当前位置的警报和蓝点:

MapName.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>


@interface MapName : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate>

@property (strong, nonatomic) IBOutlet MKMapView *MapName;
@property (strong, nonatomic) CLLocationManager *locationManager;

@end
Run Code Online (Sandbox Code Playgroud)

MapName.m

- (void)viewDidLoad
{
[super viewDidLoad];

self.locationManager = [[CLLocationManager alloc]init];
self.locationManager.delegate = self;
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
    [self.locationManager requestWhenInUseAuthorization];
}
[self.locationManager startUpdatingLocation];

//Center the map
[self gotoLocation];

//Show current position
_MapName.showsUserLocation = YES;

}
Run Code Online (Sandbox Code Playgroud)

我已将密钥NSLocationWhenIsUseUsageDescription作为字符串添加到Info.plist中.我在Xcode上仍然遇到同样的错误.

Rom*_*ius 15

这是因为:

[self.locationManager startUpdatingLocation];
Run Code Online (Sandbox Code Playgroud)

_MapName.showsUserLocation = YES;
Run Code Online (Sandbox Code Playgroud)

在调用这些用户之前,您需要检查用户是否已授予权限.还要确保在故事板上的MKMapKit中关闭用户位置(这个花了我几天的时间来追踪).

做类似的事情:

CLAuthorizationStatus authorizationStatus= [CLLocationManager authorizationStatus];

if (authorizationStatus == kCLAuthorizationStatusAuthorized ||
    authorizationStatus == kCLAuthorizationStatusAuthorizedAlways ||
    authorizationStatus == kCLAuthorizationStatusAuthorizedWhenInUse) {

    [self.locationManager startUpdatingLocation];       
    _MapName.showsUserLocation = YES;        

}
Run Code Online (Sandbox Code Playgroud)

根据您的应用程序,您可能不希望在启动时询问用户的许可,因为不建议这样做.