首页 > 代码库 > iOS 8中CLLocationManager及MKMapView showUserLocation失败的解决办法

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的描述


      完成。


参考:

http://www.cnblogs.com/tx8899/p/3989087.html

http://blog.uniba.jp/post/91830563468/ios-8

http://www.cocoachina.com/bbs/read.php?tid-217107.html

http://www.cocoachina.com/ask/questions/show/87714

http://9to5mac.com/2014/06/04/apple-improves-location-services-in-ios-8-with-when-in-use-mode-visit-monitoring/

http://derjohng.doitwell.tw/6197/%E9%9B%BB%E8%85%A6%E9%A1%9E%E5%88%A5/%E8%BB%9F%E9%AB%94%E7%AD%86%E8%A8%98/ios8-%E4%B8%8B%E5%AE%9A%E4%BD%8D-cllocationmanager-%E7%9A%84%E6%94%B9%E8%AE%8A/

http://www.cocoachina.com/ask/questions/show/113705/CLLocationManager%E8%8E%B7%E5%8F%96%E5%9C%B0%E5%9D%80%E5%81%8F%E5%B7%AE



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