iOS 8请求授权时出错

时间:2014-11-03 03:04:18

标签: objective-c iphone ios8 location-services

我在尝试请求位置服务授权时遇到很多麻烦。我知道这个论坛上还有其他帖子,但我没有用他们的解决方案来解决我的问题。

这是xCode中弹出的错误:

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

我已添加了Plist所需的

另一个重要的一点是,当我在模拟器中启动它时,我可以手动进入设置并启用位置服务,然后应用程序可以正常工作。但是,当我重新启动应用程序时它不起作用,我得到上面相同的消息。

我想提示用户选择启用位置服务。 遗憾的是,此代码不会提示对位置服务进行授权。

请帮我把头发拉了几个小时。 1

 - (void)viewDidLoad {
[super viewDidLoad];

self.locationManager = [[CLLocationManager alloc]init];
self.locationManager.delegate = self;

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



//Initialize the map and specifiy bounds
self.myMapView =[[MKMapView alloc]initWithFrame:self.view.bounds];

//specifcy resizing
self.myMapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

//show the User's location and set tracking mode
self.myMapView.showsUserLocation = YES;
self.myMapView.userTrackingMode = MKUserTrackingModeFollow;

//add the VIEW!!
[self.view addSubview:self.myMapView];



 }

这是我想要调用的函数

 - (void)requestAlwaysAuthorization
 {
     CLAuthorizationStatus status = [CLLocationManager authorizationStatus];

// If the status is denied or only granted for when in use, display an alert
if (status == kCLAuthorizationStatusAuthorizedWhenInUse || status ==        kCLAuthorizationStatusDenied) {
    NSString *title;
    title = (status == kCLAuthorizationStatusDenied) ? @"Location services are off" :   @"Background location is not enabled";
    NSString *message = @"To use background location you must turn on 'Always' in the Location Services Settings";

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title
                                                        message:message
                                                       delegate:self
                                              cancelButtonTitle:@"Cancel"
                                              otherButtonTitles:@"Settings", nil];
    [alertView show];
}
// The user has not enabled any location services. Request background authorization.
else if (status == kCLAuthorizationStatusNotDetermined) {
    [self.locationManager requestAlwaysAuthorization];
    }
}

 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
 {
  if (buttonIndex == 1) {
     // Send the user to the Settings for this app
     NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
     [[UIApplication sharedApplication] openURL:settingsURL];
 }
 }

1 个答案:

答案 0 :(得分:0)

-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status

这是关于位置服务的委托方法 您可以从status获取AuthorizationStatus。例如kCLAuthorizationStatusDenied

你只需致电

在viewdid加载中

[self.locationManager requestAlwaysAuthorization];,并在上面的委托方法中监控AuthorizationStatus,如果用户拒绝您的位置服务,则显示提醒视图