首先城市寫好沒辦法跑得先別緊張,如果你是ios8之前寫得沒辦法動是很正常的。請先去
InfoPlist.strings檔案內加這兩行
程式碼可參考這個
http://code.cocoachina.com/detail/280904
InfoPlist.strings檔案內加這兩行
"NSLocationAlwaysUsageDescription" = "Description of this";
"NSLocationWhenInUseUsageDescription" = "Description of this";
再去你程式呼叫使用到LocationManager的程式碼加上
[locationManager requestAlwaysAuthorization];
應該就可以動了。原因是因為Apple在iOS8中加強了隱私訪問權限的控制,必須調用新的方法來獲取用戶的允許。可參考連結
使用方式
1:引入CoreLocation.framework
2:取使用到的檔案加上#import <CoreLocation/CoreLocation.h>
3:宣告一個@property (strong, nonatomic) CLLocationManager *locationManager;
4:viewDidLoad寫上
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager requestAlwaysAuthorization];
//設定需要重新定位的距離差距(10m)
locationManager.distanceFilter = 10;
//設定定位時的精準度
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
5:抓經緯度
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *) oldLocation {
NSLog(@"GGGG %f",[NSString stringWithFormat:@"%f", newLocation.coordinate.longitude]);
NSLog(@"GGGG %f",[NSString stringWithFormat:@"%f", newLocation.coordinate.latitude]);
NSLog(@"GGGG %f",[NSString stringWithFormat:@"%f", newLocation.altitude]);
}
6:開始
[locationManager startUpdatingLocation];
7:結束
[locationManager stopUpdatingLocation];
程式碼可參考這個
http://code.cocoachina.com/detail/280904
全站熱搜
留言列表