iOS 8:Mapkit 错误抛出:必须调用 -[CLLocationManager requestWhenInUseAuthorization]

iOS 8 : Mapkit error throws an : Must call -[CLLocationManager requestWhenInUseAuthorization]

在 iOS 8.3 和 xCode 6.3.2 中,当我启动 Map 时,它抛出以下错误:

Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.

我已完成所有操作并按照每个步骤在 ios8 应用程序中启动地图,这是我的代码:

//viewcontroller default function set mapview and a function to find user current location.   
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    [[self otlMapView] setShowsUserLocation:YES];
    self.otlMapView.delegate = self;

    [self updateUserCurrentLocation];
}

//this function is used to fine the user current location.
-(void)updateUserCurrentLocation
{
    // Create a location manager
    self.locationManager = [[CLLocationManager alloc] init];

    // Set a delegate to receive location callbacks
    self.locationManager.delegate = self;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;

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

    // Start the location manager
    [self.locationManager startUpdatingLocation];

    [self.locationManager requestWhenInUseAuthorization];
}

#pragma mark Location Callback Method

// Wait for location callbacks
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    NSLog(@"Current Location%@", [locations lastObject]);
}

我做错了什么?

你一定要打电话给

 [self.locationManager requestWhenInUseAuthorization];

调用前

 [self.locationManager startUpdatingLocation];

还有,你设置好你的Info.plist了吗?

我假设您已经正确设置了 Info.plist。然后尝试这样的事情:

- (void)viewDidLoad {
  [self updateUserCurrentLocation];
  }

- (void)updateUserCurrentLocation {
  if (nil == locationManager) {
    locationManager = [[CLLocationManager alloc] init];
  }

  locationManager.delegate = self;

  // This part was added due to a location authorization issue on iOS8
  // See more at: http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/
  if ([self._locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
    [self._locationManager requestWhenInUseAuthorization];
  }

  self.mapView.showsUserLocation = YES;

  [locationManager startUpdatingLocation];

  CLLocation *currentLocation = locationManager.location;
  if (currentLocation) {
    PAWAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    appDelegate.currentLocation = currentLocation;
  }
  }

检查您的 info.plist 中是否有此密钥, "Privacy - Location Usage Description"、

如果是,删除它并保留 "When in use" 键。然后它应该可以工作。