iOS 8请求授权时出错



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

这是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];
 }
 }
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status

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

您只需拨打

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

相关内容

最新更新