1糖声、導入框架
2际邻、Xcode中添加“CoreLocation.framework”
3可婶、導入主頭文件
#import <CoreLocation/CoreLocation.h>
4泼舱、聲明管理器和代理
@interface ViewController ()<CLLocationManagerDelegate>
@property (nonatomic, strong) CLLocationManager* locationManager;
@end
5、初始化管理器
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
6裕循、開啟定位服務臣嚣,需要定位時調用findMe方法:
- (void)findMe {
/** 由于IOS8中定位的授權機制改變 需要進行手動授權 * 獲取授權認證净刮,兩個方法: *
[self.locationManager requestWhenInUseAuthorization];
* [self.locationManager requestAlwaysAuthorization];
*/
if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
NSLog(@"requestAlwaysAuthorization");
[self.locationManager requestAlwaysAuthorization];
}
//開始定位,不斷調用其代理方法
[self.locationManager startUpdatingLocation];
NSLog(@"start gps");
}
7硅则、代理設置
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
// 1.獲取用戶位置的對象
CLLocation *location = [locations lastObject];
CLLocationCoordinate2D coordinate = location.coordinate;
NSLog(@"緯度:%f 經(jīng)度:%f", coordinate.latitude, coordinate.longitude);
// 2.停止定位
[manager stopUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
if (error.code == kCLErrorDenied)
{
// 提示用戶出錯原因淹父,可按住Option鍵點擊 KCLErrorDenied的查看更多出錯信息,可打印error.code值查找原因所在
}
}
8怎虫、注意事項
iOS8中定位服務的變化:CLLocationManager協(xié)議方法不響應暑认,無法回掉GPS方法,不出現(xiàn)獲取權限提示大审,
解決方法如下蘸际,詳見1 詳見2
如果需要僅在前臺定位,你在調用startUpdatingLocation 前需要調用requestWhenInUseAuthorization(見設置步驟6) 如果需要在前后臺定位,你在調用startUpdatingLocation 前需要調用requestAlwaysAuthorization 在plist文件
中添加NSLocationWhenInUseUsageDescription或(與)NSLocationAlwaysUsageDescription字段:
找到info.plist文件->右擊->Open As->Source Code->在尾部的</dict>標簽之前添加以下一個或兩個:
<key>NSLocationWhenInUseUsageDescription</key><string>需要定位</string>
<key>NSLocationAlwaysUsageDescription</key><string>需要定位</string>
9、用戶隱私的保護
從iOS 6開始徒扶,蘋果在保護用戶隱私方面做了很大的加強粮彤,以下操作都必須經(jīng)過用戶批準授權:要想獲得用戶的位置,
想訪問用戶的通訊錄姜骡、日歷导坟、相機、相冊等圈澈;當想訪問用戶的隱私信息時惫周,系統(tǒng)會自動彈出一個對話框讓用戶授權。
可以在Info.plist中設置NSLocationUsageDescription說明定位的目的(Privacy - Location Usage Description) 從iOS 8開
始康栈,用戶定位分兩種情況 總是使用用戶位置:NSLocationAlwaysUsageDescription 使用應用時
定位:NSLocationWhenInUseDescription 當想訪問用戶的隱私信息時闯两,系統(tǒng)會自動彈出一個對話框讓用戶授權
/** *打開定位服務*需要在info.plist文件中添加(以下二選一,兩個都添加默
認使用NSLocationWhenInUseUsageDescription): *NSLocationWhenInUseUsageDescription
允許在前臺使用時獲取GPS的描述 *NSLocationAlwaysUsageDescription 允許永遠可獲取GPS的描述
10谅将、獲取當前軟件的定位服務狀態(tài)
if ([CLLocationManager locationServicesEnabled]
//確定用戶的位置服務啟用 &&[CLLocationManager authorizationStatus]==kCLAuthorizationStatusDenied)
//位置服務是在設置中禁用 { }
11、代碼:調用startLocation方法即可
#pragma mark Location and Delegate
- (void)startLocation {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
/** 由于IOS8中定位的授權機制改變 需要進行手動授權 * 獲取授權認證重慢,兩個方法: *
[self.locationManager requestWhenInUseAuthorization];
* [self.locationManager requestAlwaysAuthorization];
*/
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
{
NSLog(@"requestWhenInUseAuthorization");
// [self.locationManager requestWhenInUseAuthorization];
[self.locationManager requestAlwaysAuthorization]; }
//開始定位饥臂,不斷調用其代理方法 [self.locationManager startUpdatingLocation];
NSLog(@"start gps");
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
// 1.獲取用戶位置的對象
CLLocation *location = [locations lastObject];
CLLocationCoordinate2D coordinate = location.coordinate;
NSLog(@"緯度:%f 經(jīng)度:%f", coordinate.latitude, coordinate.longitude);
self.longitute = [NSNumber numberWithDouble:coordinate.longitude];
self.latitude = [NSNumber numberWithDouble:coordinate.latitude];
// 2.停止定位 [manager stopUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
if (error.code == kCLErrorDenied)
{
// 提示用戶出錯原因,可按住Option鍵點擊 KCLErrorDenied的查看更多出錯信息似踱,可打印error.code值查找原因所在
}
}