今天在寫iOS定位判斷時發(fā)現(xiàn)iOS10徹底廢掉了@"prefs:root=LOCATION_SERVICES"] 這個方法,而是重新提供了一個UIApplicationOpenSettingsURLString(ios8.0以后可用),這樣定位判斷相比IOS9.0 發(fā)生了一些變化,現(xiàn)在參考百度地圖,美團這兩個軟件,總結(jié)一下:
@"prefs:root=LOCATION_SERVICES"] 是跳轉(zhuǎn)到了 定位開關(guān)界面
UIApplicationOpenSettingsURLString 是跳轉(zhuǎn)到了定位權(quán)限界面
1.判斷比較詳細的情況
如果你的APP要求處理比較詳細的定位情況,例如你要根據(jù)不同情況執(zhí)行不同邏輯,可以采用以下代碼:
//判斷定位是否開啟
if ([CLLocationManager locationServicesEnabled])
{
// 判斷用戶是否允許程序獲取位置權(quán)限
if ([CLLocationManager authorizationStatus]==kCLAuthorizationStatusAuthorizedWhenInUse||[CLLocationManager authorizationStatus]==kCLAuthorizationStatusAuthorizedAlways)
{
//用戶允許獲取位置權(quán)限
}else
{
//用戶拒絕開啟用戶權(quán)限
UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"打開[定位服務(wù)權(quán)限]來允許[XXX]確定您的位置" message:@"請在系統(tǒng)設(shè)置中開啟定位服務(wù)(設(shè)置>隱私>定位服務(wù)>開啟)" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"去設(shè)置", nil];
alertView.delegate=self;
alertView.tag=2;
[alertView show];
}
}
else
{
UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"打開[定位服務(wù)]來允許[XXX]確定您的位置" message:@"請在系統(tǒng)設(shè)置中開啟定位服務(wù)(設(shè)置>隱私>定位服務(wù)>XXX>始終)" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"去設(shè)置", nil];
alertView.delegate=self;
alertView.tag=1;
[alertView show];
}
對應(yīng)的UIAlertView代理方法:
if (alertView.tag == 1) {
if (buttonIndex == 1) {
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10.000000) {
//跳轉(zhuǎn)到定位權(quán)限頁面
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if( [[UIApplication sharedApplication]canOpenURL:url] ) {
[[UIApplication sharedApplication] openURL:url];
}
}else {
//跳轉(zhuǎn)到定位開關(guān)界面
NSURL *url = [NSURL URLWithString:@"prefs:root=LOCATION_SERVICES"];
if( [[UIApplication sharedApplication]canOpenURL:url] ) {
[[UIApplication sharedApplication] openURL:url];
}
}
}
} else if (alertView.tag == 2) {
if (buttonIndex == 1) {
//跳轉(zhuǎn)到定位權(quán)限頁面
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if( [[UIApplication sharedApplication]canOpenURL:url] ) {
[[UIApplication sharedApplication] openURL:url];
}
}
}
2.簡單的暴力的判斷情況,代碼如下,我發(fā)現(xiàn)美團和百度地圖是這樣做的:
if([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied)
{
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"打開[定位服務(wù)]來允許[XXX]確定您的位置" message:@"請在系統(tǒng)設(shè)置中開啟定位服務(wù)(設(shè)置>隱私>定位服務(wù)>開啟)" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"設(shè)置" , nil];
alertView.delegate = self;
alertView.tag = 1;
[alertView show];
}
而且對應(yīng)的跳轉(zhuǎn)方式也很簡單直接,這點參考了美團和百度地圖,直接跳轉(zhuǎn)到了程序定位權(quán)限設(shè)置頁面:
if (alertView.tag == 1) {
if (buttonIndex == 1) {
//跳轉(zhuǎn)到定位權(quán)限頁面
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if( [[UIApplication sharedApplication]canOpenURL:url] ) {
[[UIApplication sharedApplication] openURL:url];
}
}
}
3.順便簡單總結(jié)一下這個CLAuthorizationStatus 這個枚舉,方便大家使用:
一、第一個枚舉值:kCLAuthorizationStatusNotDetermined的意思是:定位服務(wù)授權(quán)狀態(tài)是用戶沒有決定是否使用定位服務(wù)。
二、第二個枚舉值:kCLAuthorizationStatusRestricted的意思是:定位服務(wù)授權(quán)狀態(tài)是受限制的??赡苁怯捎诨顒酉拗贫ㄎ环?wù),用戶不能改變。這個狀態(tài)可能不是用戶拒絕的定位服務(wù)。
三、第三個枚舉值:kCLAuthorizationStatusDenied的意思是:定位服務(wù)授權(quán)狀態(tài)已經(jīng)被用戶明確禁止,或者在設(shè)置里的定位服務(wù)中關(guān)閉。
四、第四個枚舉值:kCLAuthorizationStatusAuthorizedAlways的意思是:定位服務(wù)授權(quán)狀態(tài)已經(jīng)被用戶允許在任何狀態(tài)下獲取位置信息。包括監(jiān)測區(qū)域、訪問區(qū)域、或者在有顯著的位置變化的時候。
五、第五個枚舉值:kCLAuthorizationStatusAuthorizedWhenInUse的意思是:定位服務(wù)授權(quán)狀態(tài)僅被允許在使用應(yīng)用程序的時候。
六、第六個枚舉值:kCLAuthorizationStatusAuthorized的意思是:這個枚舉值已經(jīng)被廢棄了。他相當于kCLAuthorizationStatusAuthorizedAlways這個值。
總結(jié)
我發(fā)現(xiàn),再iOS10中集成百度SDK后,如果你在第一次打開APP時,彈出定位權(quán)限提示時你選擇了允許,下次你使用定位服務(wù)時定位開關(guān)是關(guān)閉的,百度地圖會給你彈出提示,點擊設(shè)置是可以跳轉(zhuǎn)到定位開關(guān)頁面,但是沒有想明白怎么做的。
寫篇東西不容易,喜歡就點個贊吧!