Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 请求授权时出现iOS 8错误_Objective C_Iphone_Ios8_Location Services - Fatal编程技术网

Objective c 请求授权时出现iOS 8错误

Objective c 请求授权时出现iOS 8错误,objective-c,iphone,ios8,location-services,Objective C,Iphone,Ios8,Location Services,我在尝试请求位置服务授权时遇到很多问题。我知道这个论坛上还有其他帖子,但我没有用他们的解决方案解决我的问题 这是xCode中出现的错误: Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwa

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

这是xCode中出现的错误:

Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.
我已经为Plist添加了两个必需的

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

我想向用户提示启用位置服务的选项。 不幸的是,此代码不会提示位置服务的授权

请帮帮我,我已经拔头发好几个小时了。 一,

这是我想调用的函数

 - (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];
 }
 }
这是关于位置服务的委托方法 您可以从
status
获取您的授权状态。例如
kclauauthorizationstatusdenied

你只要打电话


[self.locationManager请求始终授权]
在viewdid load中,并在上面的委托方法中监视
授权状态
,如果用户拒绝您的位置服务,则在我实现此方法并调用DidChangeAuthorizationStatus时显示alertview

,它不会进入此委托方法。它是在viewDidLoad完成后转到此方法,还是在状态更改后直接转到方法。显然,即使在viewdidload后的任何时候调用此requestAlwaysAuthorization之后,位置服务也没有设置,但将调用此委托方法。您只需在ViewDidLaod中调用[self.locationManager requestAlwaysAuthorization]。不要使用if([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)];第一次安装此应用程序时,将显示一个警报,通知用户此应用程序需要loaction Service Do not call delegate方法,它将在应用程序打开时自动调用。您只需将代码写入delegate方法中,即可使用不同的密码进行操作
 - (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