關於介入地圖相關功能后會遇到類似定位的子功能,由此引來了此定位權限授權相關.
首先,需要導入 CoreLocation 的框架並創建管理對象從而實現后續的相關操作;
#import <CoreLocation/CoreLocation.h>
其中里面會包含一些參數屬性方法等,例如:
1)是否開啟位置服務
/* * locationServicesEnabled * * Discussion: * Determines whether the user has location services enabled. * If NO, and you proceed to call other CoreLocation API, user will be prompted with the warning * dialog. You may want to check this property and use location services only when explicitly requested by the user. */ + (BOOL)locationServicesEnabled API_AVAILABLE(ios(4.0), macos(10.7));
2)設置定位所期望的精准度,其中會有響應的枚舉值可供選擇,但精准度越高所消耗的設備電源性能也會隨之受其影響,例如:導航模式
/* * desiredAccuracy * * Discussion: * The desired location accuracy. The location service will try its best to achieve * your desired accuracy. However, it is not guaranteed. To optimize * power performance, be sure to specify an appropriate accuracy for your usage scenario (eg, * use a large accuracy value when only a coarse location is needed). Use kCLLocationAccuracyBest to * achieve the best possible accuracy. Use kCLLocationAccuracyBestForNavigation for navigation. * The default value varies by platform. */ @property(assign, nonatomic) CLLocationAccuracy desiredAccuracy; 精准度 desiredAccuracy 所對應的枚舉值相關: /* * kCLLocationAccuracy<x> * * Discussion: * Used to specify the accuracy level desired. The location service will try its best to achieve * your desired accuracy. However, it is not guaranteed. To optimize * power performance, be sure to specify an appropriate accuracy for your usage scenario (eg, * use a large accuracy value when only a coarse location is needed). */ extern const CLLocationAccuracy kCLLocationAccuracyBestForNavigation API_AVAILABLE(ios(4.0), macos(10.7));// 最近 extern const CLLocationAccuracy kCLLocationAccuracyBest;// 最優 extern const CLLocationAccuracy kCLLocationAccuracyNearestTenMeters;// 十米 extern const CLLocationAccuracy kCLLocationAccuracyHundredMeters;// 百米 extern const CLLocationAccuracy kCLLocationAccuracyKilometer;// 千米 extern const CLLocationAccuracy kCLLocationAccuracyThreeKilometers;// 三千米
3)設置定位距離過濾的參數,若此次與上次定位所產生的距離差值大於或等於此設定值時則會調用代理方法
/* * distanceFilter * * Discussion: * Specifies the minimum update distance in meters. Client will not be notified of movements of less * than the stated value, unless the accuracy has improved. Pass in kCLDistanceFilterNone to be * notified of all movements. By default, kCLDistanceFilterNone is used. */ @property(assign, nonatomic) CLLocationDistance distanceFilter;
4)開始 & 停止位置的更新
/* * startUpdatingLocation * * Discussion: * Start updating locations. */ - (void)startUpdatingLocation API_AVAILABLE(watchos(3.0)) API_UNAVAILABLE(tvos); /* * stopUpdatingLocation * * Discussion: * Stop updating locations. */ - (void)stopUpdatingLocation;
5)設置代理(delegate)后的一些常用代理方法
locationManager.delegate = self; - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations { NSLog(@"獲取定位信息 --- 成功"); } - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { NSLog(@"獲取定位信息 --- 失敗"); }
其次,聲明全局的變量 CLLocationManager,此處需要注意若使用局部變量的方式會調用授權方法失敗;
/** 位置管理*/ CLLocationManager *locationManager;
再其次,初始化設置代理並配置系統位置權限授權操作相關
注:此處需要判斷一下系統的版本號,避免異常閃退,因屬性是在系統8.0基礎之上才可以使用
#pragma mark - ****************************** 獲取位置驗證權限 /** 獲取位置驗證權限(作用域: 地圖 & 定位相關) @param vc 當前視圖控件 */ - (void)YHGetLocationPermissionVerifcationWithController:(UIViewController *)vc { BOOL enable = [CLLocationManager locationServicesEnabled]; NSInteger state = [CLLocationManager authorizationStatus]; if (!enable || 2 > state) {// 尚未授權位置權限 if (8 <= [[UIDevice currentDevice].systemVersion floatValue]) { NSLog(@"系統位置權限授權彈窗"); // 系統位置權限授權彈窗 locationManager = [[CLLocationManager alloc] init]; locationManager.delegate = self; [locationManager requestAlwaysAuthorization]; [locationManager requestWhenInUseAuthorization]; } } else { if (state == kCLAuthorizationStatusDenied) {// 授權位置權限被拒絕 NSLog(@"授權位置權限被拒絕"); UIAlertController *alertCon = [UIAlertController alertControllerWithTitle:@"提示" message:@"訪問位置權限暫未授權" preferredStyle:UIAlertControllerStyleAlert]; [alertCon addAction:[UIAlertAction actionWithTitle:@"暫不設置" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { }]]; [alertCon addAction:[UIAlertAction actionWithTitle:@"設置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { dispatch_after(0.2, dispatch_get_main_queue(), ^{ NSURL *url = [[NSURL alloc] initWithString:UIApplicationOpenSettingsURLString];// 跳轉至系統定位授權 if( [[UIApplication sharedApplication] canOpenURL:url]) { [[UIApplication sharedApplication] openURL:url]; } }); }]]; [vc presentViewController:alertCon animated:YES completion:^{ }]; } } }
GitHub: https://github.com/survivorsfyh/YHTools/tree/master/YHAccessAuthorization
______
以上便是此次內容小結,項目中用到的功能不多沒有深挖,還有很多可拓展的地方,有什么不足還望多多指點!