您的位置:首页 > 移动开发 > IOS开发

Core Location Manager在IOS8中的改变

2014-10-24 10:09 253 查看
以前的代码在ios7里运行得好好的,到ios8就不行了,没有报错没有警告,只在运行时有log显示 Trying to start MapKit location updates without prompting for location authorization. Must
call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.

解决方法分两步:

首先, 在Info.plist文件里添加NSLocationWhenInUseUsageDescription 或 NSLocationAlwaysUsageDescription ,根据你的需要,添加两者或其中的一个.这两个key都是String类型的,value可以为空. 

接下来,就要在你的代码里加上 requestWhenInUseAuthorization 和   requestAlwaysAuthorization方法(两者或其一).代码如下:

self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
// 检查是否为ios8,直接调用在7下面会蹦掉
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[self.locationManager requestWhenInUseAuthorization];
}
[self.locationManager startUpdatingLocation];

到这里,问题应该解决了,运行程序试试看.

但如果你是使用的MKMapView,接着往下看:

如果使用MapView的showsUserLocation属性来定位,除了在info.plist里添加key之外,还需要在ViewController里加上一个CLLocationManager的属性,然后在showsUserLocation之前,加上上面的代码,问题就解决了!代码如下:

self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
// Check for iOS 8
if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[self.locationManager requestAlwaysAuthorization];
}
self.mapView.showsUserLocation = YES;
       

     
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: