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

标签 iphone mapkit ios8 core-location xcode6

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

MapName.h

#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

map 名称.m

- (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 上仍然遇到同样的错误。

最佳答案

这是由于:

[self.locationManager startUpdatingLocation];

_MapName.showsUserLocation = YES;

在调用这些之前,您需要检查用户是否已授予权限。还要确保关闭 Storyboard 上 MKMapKit 中的用户位置(这花了我几天时间才找到)。

做类似的事情:

CLAuthorizationStatus authorizationStatus= [CLLocationManager authorizationStatus];

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

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

}

根据您的应用,您可能不想在启动时请求用户的许可,因为不建议这样做。

关于iphone - -[CLLocationManager requestWhenInUseAuthorization] 或 -[CLLocationManager requestAlwaysAuthorization] 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25950363/

相关文章:

ios - 检查是否按下了标注 View (mapBox iOS SDK)

iphone - 如何使用苹果的 KML 查看器示例代码显示多边形

ios - 使用 MKAnnotationView 委托(delegate)时引脚消失

ios - 无法识别的选择器 isPitched 被调用

ios - 隐藏纵向的 UISplitViewController 覆盖

iphone - Android相当于iphone索引的UITableView

iphone - 为什么我的 UILabel 没有被更改?

iphone - iOS:从 UIView 创建 PDF

ios - 支持 iPhone 6 和 iPhone 6+,针对 iPad 纵向和横向使用不同的启动/启动屏幕图像

swift - ios8机会性后台获取GET请求