天天看点

iOS 8中CLLocationManager及MKMapView showUserLocation失败的解决的方法

          用XCode 6编译的原来XCode 5.1.1写的程序时,发现原来写的CLLocationManager定位的代码以及MKmapView的showUserLocation失效。查了一下,XCode 6选用iOS 8 SDK编译app的话,须要调用CLLocationManage 的requestAlwaysAuthorization 方法。

操作过程例如以下:

1. 在AppDelegate的didFinishLaunchingWithOptions:方法中 [self.window makeKeyAndVisible]; 之后加入下面代码

CLLocationManager *locationManager = [[CLLocationManager alloc] init];
    // 判斷是否 iOS 8
    if([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
        [locationManager requestAlwaysAuthorization]; // 永久授权
        [locationManager requestWhenInUseAuthorization]; //使用中授权
    }
    [locationManager startUpdatingLocation];      

2. 在 info.plist里增加:

NSLocationWhenInUseDescription,同意在前台获取GPS的描写叙述

NSLocationAlwaysUsageDescription,同意在后台获取GPS的描写叙述