iOS原生類(lèi)獲取定位授權(quán)是通過(guò)CoreLocation框架中的CLLocationManager類(lèi)完成的骏令,這其中可以解決兩大類(lèi)場(chǎng)景的授權(quán)問(wèn)題:
1、原生開(kāi)發(fā)的授權(quán)
2、網(wǎng)頁(yè)混合開(kāi)發(fā)的授權(quán)。使用webview加載網(wǎng)頁(yè)高职,授權(quán)歸入APP授權(quán)钩乍;在瀏覽器中加載頁(yè)面辞州,授權(quán)歸入瀏覽器授權(quán)。
一寥粹、概述獲取權(quán)限過(guò)程
1变过、調(diào)用authorizationStatus,獲取狀態(tài)涝涤,只要不是允許狀態(tài)媚狰,進(jìn)行下個(gè)步驟
2、CLLocationManager創(chuàng)建實(shí)例阔拳,并設(shè)置代理
3崭孤、保持CLLocationManager實(shí)例的生命周期到任務(wù)結(jié)束
4、如果授權(quán)狀態(tài)不是允許,則獲取授權(quán)或者跳轉(zhuǎn)設(shè)置頁(yè)面繼續(xù)修改
5辨宠、根據(jù)需要開(kāi)啟不同的定位服務(wù)
二遗锣、獲取授權(quán)狀態(tài)
//次方法返回值為CLAuthorizationStatus的枚舉類(lèi)型,如下所示
[CLLocationManager authorizationStatus];
typedef NS_ENUM(int, CLAuthorizationStatus) {
//未做選擇:未選允許嗤形、拒絕(例如彈出提示框精偿,用戶(hù)之間退出APP時(shí)會(huì)發(fā)生)
kCLAuthorizationStatusNotDetermined = 0,
//受限制:未授權(quán),主動(dòng)現(xiàn)在定位服務(wù)赋兵,用戶(hù)無(wú)法改變?cè)摖顟B(tài)(可能沒(méi)有否認(rèn)個(gè)人授權(quán))
kCLAuthorizationStatusRestricted,
// 拒絕:用戶(hù)拒絕授權(quán)笔咽,可在設(shè)置在修改
kCLAuthorizationStatusDenied,
//授權(quán)且在未使用APP時(shí)使用定位
kCLAuthorizationStatusAuthorizedAlways NS_ENUM_AVAILABLE(NA, 8_0),
// 授權(quán)且當(dāng)APP使用中使用定位
kCLAuthorizationStatusAuthorizedWhenInUse NS_ENUM_AVAILABLE(NA, 8_0),
// 已廢棄:不做解釋?zhuān)诠俜絛emo中也不在關(guān)注該值
kCLAuthorizationStatusAuthorized NS_ENUM_DEPRECATED(10_6, NA, 2_0, 8_0, "Use kCLAuthorizationStatusAuthorizedAlways") __TVOS_PROHIBITED __WATCHOS_PROHIBITED = kCLAuthorizationStatusAuthorizedAlways
};
根據(jù)當(dāng)前授權(quán)狀態(tài)做響應(yīng)的交互處理:
#pragma mark - 檢查授權(quán)狀態(tài)
- (void)checkLocationServicesAuthorizationStatus {
[self reportLocationServicesAuthorizationStatus:[CLLocationManager authorizationStatus]];
}
- (void)reportLocationServicesAuthorizationStatus:(CLAuthorizationStatus)status
{
if(status == kCLAuthorizationStatusNotDetermined)
{
//未決定,繼續(xù)請(qǐng)求授權(quán)
[self requestLocationServicesAuthorization];
}
else if(status == kCLAuthorizationStatusRestricted)
{
//受限制霹期,嘗試提示然后進(jìn)入設(shè)置頁(yè)面進(jìn)行處理(根據(jù)API說(shuō)明一般不會(huì)返回該值)
[self alertViewWithMessage];
}
else if(status == kCLAuthorizationStatusDenied)
{
//拒絕使用叶组,提示是否進(jìn)入設(shè)置頁(yè)面進(jìn)行修改
[self alertViewWithMessage];
}
else if(status == kCLAuthorizationStatusAuthorizedWhenInUse)
{
//授權(quán)使用,不做處理
}
else if(status == kCLAuthorizationStatusAuthorizedAlways)
{
//始終使用历造,不做處理
}
}
#pragma mark - Helper methods
- (void)alertViewWithMessage {
UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"定位服務(wù)未開(kāi)啟" message:@"請(qǐng)?jiān)谙到y(tǒng)設(shè)置中開(kāi)啟服務(wù)" delegate:self cancelButtonTitle:@"暫不" otherButtonTitles:@"去設(shè)置", nil];
[alter show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
}
else
{
//進(jìn)入系統(tǒng)設(shè)置頁(yè)面扶叉,APP本身的權(quán)限管理頁(yè)面
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}
}
三、獲取授權(quán)
#pragma mark - 獲取授權(quán)
- (void)requestLocationServicesAuthorization
{
//CLLocationManager的實(shí)例對(duì)象一定要保持生命周期的存活
if (!self.locationManager) {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
}
[self.locationManager requestWhenInUseAuthorization];
[self.locationManager startUpdatingLocation];
}
#pragma mark - CLLocationMangerDelegate methods
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
[self.locationManager stopUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
[self.locationManager stopUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
[self reportLocationServicesAuthorizationStatus:status];
}
關(guān)于以上代碼幾點(diǎn)說(shuō)明
1帕膜、CLLocationManager的對(duì)象實(shí)例一定要保證生命周期存在知道任務(wù)結(jié)束枣氧,因?yàn)槎ㄎ环?wù)的任務(wù)執(zhí)行時(shí)異步,使用局部變量不能保證定位服務(wù)正常進(jìn)行垮刹。
2达吞、iOS8系統(tǒng)之后一定要在info.plist文件中添加NSLocationWhenInUseUsageDescription或者NSLocationAlwaysUsageDescription,根據(jù)需要選擇其一或者其二(決定了授權(quán)設(shè)置頁(yè)面中選項(xiàng)數(shù)目)荒典,內(nèi)容選填酪劫。
3、requestWhenInUseAuthorization或者requestAlwaysAuthorization方法寺董,要在使用定位服務(wù)(比如startUpdatingLocation)之前調(diào)用覆糟。
4、APP進(jìn)入后臺(tái)運(yùn)行的時(shí)候遮咖,開(kāi)啟定位服務(wù)會(huì)失敗滩字。
5、當(dāng)進(jìn)行授權(quán)選擇后御吞,會(huì)調(diào)用didChangeAuthorizationStatus方法麦箍,此時(shí)重新走一次檢測(cè)授權(quán)狀態(tài)過(guò)程。
四陶珠、調(diào)用方法開(kāi)始整個(gè)獲取權(quán)限流程
- (void)viewDidLoad {
[super viewDidLoad];
[self checkLocationServicesAuthorizationStatus];
}
只需要調(diào)用次方法即可挟裂。
五、最后的話
定位服務(wù)還有其他內(nèi)容揍诽,學(xué)習(xí)了其他內(nèi)容會(huì)繼續(xù)更新诀蓉,詳情請(qǐng)參考官方文檔栗竖,關(guān)于其他系統(tǒng)服務(wù)權(quán)限的獲取,可以參考另一篇文章和官方Demo渠啤。