【问题标题】:Not Getting Permission From User in MapKit在 MapKit 中未获得用户的许可
【发布时间】:2016-07-04 19:30:27
【问题描述】:

必须先致电-[CLLocationManager requestWhenInUseAuthorization]-[CLLocationManager requestAlwaysAuthorization]。实际上我已经在 Didload 中调用了这个方法,甚至在info.plist 中添加了这个方法,仍然出现这个错误。

    - (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"MapView";

    [MapView setMapType:MKMapTypeStandard];
    [MapView setZoomEnabled:YES];
    [MapView setScrollEnabled:YES];
    [MapView setShowsUserLocation:YES];
    MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
    region.center.latitude = 23.0804 ;
    region.center.longitude = 72.5241;
    region.span.longitudeDelta = 0.01f;
    region.span.latitudeDelta = 0.01f;
    [MapView setRegion:region animated:YES];

    [MapView setDelegate:self];

    CLLocationManager *locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone; //whenever we move
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    [locationManager requestAlwaysAuthorization];
    [locationManager requestWhenInUseAuthorization];

    [locationManager startUpdatingLocation];

    Display *ann = [[Display alloc] init];
    ann.title = @" Gujarat";
    ann.subtitle = @"High Court";
    ann.coordinate = region.center;
    [MapView addAnnotation:ann];

}
-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:
(id <MKAnnotation>)annotation {
    MKPinAnnotationView *pinView = nil;
    if(annotation != MapView.userLocation)
    {
        static NSString *defaultPinID = @"com.invasivecode.pin";
        pinView = (MKPinAnnotationView *)[MapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
        if ( pinView == nil ) pinView = [[MKPinAnnotationView alloc]
                                         initWithAnnotation:annotation reuseIdentifier:defaultPinID];

        pinView.pinColor = MKPinAnnotationColorRed;
        pinView.canShowCallout = YES;
        pinView.animatesDrop = YES;
        pinView.draggable = YES;
    }
    else {
        [MapView.userLocation setTitle:@"I am here"];
    }
    return pinView;
}


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

{

    if (status == kCLAuthorizationStatusAuthorizedWhenInUse)
    {
        [locationManager startUpdatingLocation];
    }
    else if (status == kCLAuthorizationStatusDenied)
    {
        //Alert to show for user if any when status is declined
    }
    else
        NSLog(@"Wrong location status");
}

并在 Annotation 的 Display 类中

<MKAnnotation> {

    CLLocationCoordinate2D coordinate; 
    NSString *title; 
    NSString *subtitle;
}
@property (nonatomic, assign) CLLocationCoordinate2D coordinate; 
@property (nonatomic, copy) NSString *title; 
@property (nonatomic, copy) NSString *subtitle;

这里的代码没有显示提示用户是否允许访问其位置,因此我们无法获得所需位置的位置。请帮我从昨天开始。

【问题讨论】:

  • 仍然收到此错误。 ...?
  • 是的还是不知道我卡在哪里了
  • 调用 -[CLLocationManager requestWhenInUseAuthorization] 或 -[CLLocationManager requestAlwaysAuthorization],但不能同时调用,因为它没有意义
  • 由于 nick 的回答由于某种原因对您没有帮助,请提供简化的示例应用程序以便我们保存

标签: ios objective-c dictionary


【解决方案1】:

在您的应用中检查以下内容。

1) 创建一个CLLocation Manager 的类对象。 因为如果您创建 CLLocation 管理器的本地对象,则 CLLocationManager 对象在用户响应对话框之前被释放。

2) 进入设置 > 隐私 > 定位服务。并检查您的应用是否列在位置服务列表中。如果是,则点击您的应用并进入内部检查开关是否打开。

3) 使用检查是否支持 requestAlwaysAuthorization:

if ([_locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
    [_locationManager requestAlwaysAuthorization];
}

4) 尝试检查位置管理器的状态使用

-(void)checkStatus{

CLAuthorizationStatus status = [CLLocationManager authorizationStatus];

if (status==kCLAuthorizationStatusNotDetermined) {
_status.text = @"Not Determined";
}

if (status==kCLAuthorizationStatusDenied) {
_status.text = @"Denied";
}

if (status==kCLAuthorizationStatusRestricted) {
_status.text = @"Restricted";
}

if (status==kCLAuthorizationStatusAuthorizedAlways) {
_status.text = @"Always Allowed";
}

if (status==kCLAuthorizationStatusAuthorizedWhenInUse) {
_status.text = @"When In Use Allowed";
  }

}

更多详情请访问

1) http://www.devfright.com/location-authorization-ios-8/
2)http://nshipster.com/core-location-in-ios-8/

【讨论】:

  • 我得到状态未确定
  • 您能否在设置 > 隐私 > 定位服务中进行检查。您的应用程序是否在此处列出?您也可以尝试从模拟器或设备中删除应用程序并重新加载您的应用程序。
  • 仍然无法正常工作我已经检查了设置并多次删除.. 位置管理器未确定时的解决方案是什么
  • 嘿,你有没有让你的 cllocationmanager 成为一个全局变量?你可以查看我的演示应用程序。 dropbox.com/s/x92puk6k2gnwczi/TestApp.zip?dl=0
  • 嗨纳伦德拉,这是您的演示,正确答案dropbox.com/s/tgszxq8hlev8uyj/MapViewDemo_Corrected.zip?dl=0
【解决方案2】:

你需要做两件事:

  1. CLLocationManager *locationManager 声明为全局变量。因为位置管理器将在范围结束时被释放。因此,一旦范围结束,要显示的授权弹出窗口将隐藏。

  2. [locationManager startUpdatingLocation] 应在用户授权状态后立即写入。

    进口

    进口

    @interface ViewController () <CLLocationManagerDelegate>
    
    {
       CLLocationManager *locationManager;
    }
    
    @implementation ViewController
    - (void)viewDidLoad {
        [super viewDidLoad]; 
    
        [MapView setMapType:MKMapTypeStandard];
        [MapView setZoomEnabled:YES];
        [MapView setScrollEnabled:YES];
        [MapView setShowsUserLocation:YES];
        MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
        region.center.latitude = 23.0804 ;
        region.center.longitude = 72.5241;
        region.span.longitudeDelta = 0.01f;
        region.span.latitudeDelta = 0.01f;
        [MapView setRegion:region animated:YES];
    
        locationManager = [[CLLocationManager alloc] init];
        locationManager.delegate = self;
        locationManager.distanceFilter = kCLDistanceFilterNone; //whenever we move
        locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    
    
        if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
        {
            [locationManager requestWhenInUseAuthorization];
        }
    
        Display *ann = [[Display alloc] init];
        ann.title = @" Gujarat";
        ann.subtitle = @"High Court";
        ann.coordinate = region.center;
        [MapView addAnnotation:ann];
    }
    
    -(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
            if (status == kCLAuthorizationStatusAuthorizedWhenInUse)
            {
                [locationManager startUpdatingLocation];
            }
            else if (status == kCLAuthorizationStatusDenied)
            {
            //Alert to show for user if any when status is declined
            }
            else
                NSLog(@"Wrong location status");
    }
    

【讨论】:

    【解决方案3】:

    我能在你的 .plist 截图中看到的是

    NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription key in Info.plist 
    

    不见了。这是为了在您的应用程序首次启动时显示提示。 添加这些可以解决问题。

    【讨论】:

    • NSLocationAlwaysUsageDescription 我也添加了那个方法,它的拼写是正确的..我检查了很多次..
    • 你能从 plist 中删除 NSLocationWhenInUseUsageDescription 并尝试通过重置模拟器运行吗?
    • 我已经做过很多次了。我什至没有通过在运行时选择(调试)来获取用户位置。
    • 第一次安装时是否收到提示信息?还要检查您是在真实设备中还是仅在模拟器中遇到 GPS 定位问题?
    • 不,我没有收到任何关于访问该位置的警报消息,我们正面临 GPS 定位问题
    【解决方案4】:

    实际上有两个 info.plist,我正在更改 Mapview 演示测试 plist 文件中的值。当我在 Info.plist 中添加 NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription 键时它起作用了..

    【讨论】:

      猜你喜欢
      • 2020-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多