0

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

这是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];
 }
 }
4

1 回答 1

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

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

你只需调用

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

于 2014-11-03T03:20:26.083 回答