CLLocationManagerDelegate不調用didUpdateLocations (地圖),cllocationmanager
這是因為xcode升級造成的定位使用權限設定問題。
升級xcode6以後開啟以前xcode5工程,程式不能定位。工程升級到xcode6編譯時間需要iOS8 要自己寫授權,不然沒許可權定位。
解決方案:
首先在 info.plist裡加入對應的預設欄位 ,值設定為YES(前台定位寫上邊欄位,前後台定位寫下邊欄位)
NSLocationWhenInUseUsageDescription //允許在前台擷取GPS的描述
NSLocationAlwaysUsageDescription //允許在前、後台擷取GPS的描述
後方附圖
先上代碼:
?
123456789101112131415161718192021222324252627282930313233343536 |
#import "ViewController.h" #import <CoreLocation/CoreLocation.h> @interface ViewController ()<CLLocationManagerDelegate> @property ( nonatomic , strong) CLLocationManager *locationManager; @end @implementation ViewController // 1. 懶載入初始化: - (CLLocationManager *)locationManager{ if (!_locationManager){ self .locationManager = [[CLLocationManager alloc] init]; self .locationManager.delegate = self ; } return _locationManager; } - ( void )viewDidLoad { [ super viewDidLoad]; // 2. 調用請求: if ([[[UIDevice currentDevice] systemVersion] doubleValue] > 8.0) { //設定定位許可權 僅ios8有意義 [ self .locationManager requestWhenInUseAuthorization]; // 前台定位 // [locationManager requestAlwaysAuthorization];// 前後台同時定位 } [ self .locationManager startUpdatingLocation]; } // 3.代理方法 - ( void )locationManager:(CLLocationManager *)manager didUpdateLocations:( NSArray *)locations{ NSLog (@ "%ld" ,locations.count); } @end |
圖片:圖.png