首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >-[CLLocationManager requestWhenInUseAuthorization]或-[CLLocationManager requestAlwaysAuthorization]错误

-[CLLocationManager requestWhenInUseAuthorization]或-[CLLocationManager requestAlwaysAuthorization]错误
EN

Stack Overflow用户
提问于 2014-09-20 15:32:10
回答 6查看 37.1K关注 0票数 13

这是我的代码,显示了地图上当前位置的警报和蓝点:

MapName.h

代码语言:javascript
复制
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>


@interface MapName : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate>

@property (strong, nonatomic) IBOutlet MKMapView *MapName;
@property (strong, nonatomic) CLLocationManager *locationManager;

@end

MapName.m

代码语言:javascript
复制
- (void)viewDidLoad
{
[super viewDidLoad];

self.locationManager = [[CLLocationManager alloc]init];
self.locationManager.delegate = self;
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
    [self.locationManager requestWhenInUseAuthorization];
}
[self.locationManager startUpdatingLocation];

//Center the map
[self gotoLocation];

//Show current position
_MapName.showsUserLocation = YES;

}

我已经将键NSLocationWhenIsUseUsageDescription作为字符串添加到Info.plist中。我在Xcode上仍然会遇到同样的错误。

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2014-10-01 23:24:22

其原因是:

代码语言:javascript
复制
[self.locationManager startUpdatingLocation];

代码语言:javascript
复制
_MapName.showsUserLocation = YES;

在调用这些文件之前,您需要检查用户是否已授予权限。还要确保关闭故事板上的MKMapKit中的用户位置(这一次花了我几天的时间追踪)。

做以下事情:

代码语言:javascript
复制
CLAuthorizationStatus authorizationStatus= [CLLocationManager authorizationStatus];

if (authorizationStatus == kCLAuthorizationStatusAuthorized ||
    authorizationStatus == kCLAuthorizationStatusAuthorizedAlways ||
    authorizationStatus == kCLAuthorizationStatusAuthorizedWhenInUse) {

    [self.locationManager startUpdatingLocation];       
    _MapName.showsUserLocation = YES;        

}

根据您的应用程序,您可能不想要求用户的许可启动,因为这是不推荐的。

票数 15
EN

Stack Overflow用户

发布于 2014-09-22 16:25:18

错误消息是非常字面的。在获得授权之前,不要调用[self.locationManager startUpdatingLocation]。根据文档,您的[self.locationManager requestWhenInUseAuthorization]是异步的。

当当前授权状态为kCLAuthorizationStatusNotDetermined时,此方法异步运行,并提示用户授予应用程序使用位置服务的权限。

这意味着,您既提示进行访问,又同时启动扫描。

相反,尝试实现-[CLLocationManagerDelegate locationManager:didChangeAuthorizationStatus:]并在确定有授权后在那里启动扫描。

票数 4
EN

Stack Overflow用户

发布于 2016-03-31 10:22:43

关于迅捷的:

代码语言:javascript
复制
  let locationManager: CLLocationManager = CLLocationManager()
  let authorizationStatus = CLLocationManager.authorizationStatus()


  override func viewDidLoad() {
    super.viewDidLoad()
    if(authorizationStatus == .AuthorizedWhenInUse || authorizationStatus == .AuthorizedAlways) {
       locationManager.startUpdatingLocation()
    }
    else
    {
      locationManager.requestWhenInUseAuthorization()
    }
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters

}

不要忘记将以下键添加到Info.plist文件中:

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25950363

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档