IOS開發(fā)中的權(quán)限問題

參考http://mt.sohu.com/20160707/n458265498.shtml

對定位權(quán)限、定位是否開啟的判斷

// ios8.0+需要請求授權(quán)
if (isIOS(8.0)) {
    
    // 要在此處, 請求授權(quán), 但是請求哪個權(quán)限, 沒法確定, 靠其他開發(fā)者確定;
    
    // 1. 獲取info.plist 文件內(nèi)容
    NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
    
    //            NSLog(@"%@", infoDic);
    
    // 2. 獲取其他開發(fā)人員, 填寫的key
    NSString *always = infoDic[@"NSLocationAlwaysUsageDescription"];
    NSString *whenInUse = infoDic[@"NSLocationWhenInUseUsageDescription"];
    
    if ([always length] > 0) {
        [_locationM requestAlwaysAuthorization];
    } else if ([whenInUse length] > 0) {
        [_locationM requestWhenInUseAuthorization];
        
        // 在前臺定位授權(quán)狀態(tài)下, 必須勾選后臺模式location udpates才能獲取用戶位置信息
        NSArray *services = infoDic[@"UIBackgroundModes"];
        
        if (![services containsObject:@"location"]) {
            DBLog(@"友情提示: 當(dāng)前狀態(tài)是前臺定位授權(quán)狀態(tài), 如果想要在后臺獲取用戶位置信息, 必須勾選后臺模式 location updates");
        } else {
            if (isIOS(9.0)) {
                _locationM.allowsBackgroundLocationUpdates = YES;
            }
        }
    } else {
        DBLog(@"錯誤---如果在iOS8.0之后定位, 必須在info.plist, 配置NSLocationWhenInUseUsageDescription 或者 NSLocationAlwaysUsageDescription");
    }
}

// 判斷是否授權(quán)
CLAuthorizationStatus authStatus = [CLLocationManager authorizationStatus];

if (authStatus == kCLAuthorizationStatusRestricted || authStatus == kCLAuthorizationStatusDenied) {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"沒有位置權(quán)限" message:@"請去設(shè)置-隱私-定位服務(wù)中對52度授權(quán)" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"現(xiàn)在就去" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"以后再說" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:cancelAction];
    [alertController addAction:okAction];
    
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
}
}

// 定位服務(wù)未開啟
if (![CLLocationManager locationServicesEnabled]) {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"打開“定位服務(wù)”來允許“52度”確定您的位置" message:@"52度將獲取您的位置,為您提供更精確的服務(wù)" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"設(shè)置" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=LOCATION_SERVICES"]];
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:cancelAction];
    [alertController addAction:okAction];
    
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 在此特此聲明:一下所有鏈接均來自互聯(lián)網(wǎng),在此記錄下我的查閱學(xué)習(xí)歷程,感謝各位原創(chuàng)作者的無私奉獻(xiàn) ! 技術(shù)一點一點積...
    遠(yuǎn)航的移動開發(fā)歷程閱讀 11,214評論 12 197
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,242評論 25 708
  • Down to lunch(下面簡稱DTL)最近很火,在app store美國區(qū)的社交榜排名前三,一度超過了fb,...
    cys的tony閱讀 1,089評論 1 1
  • 江陰半島北依福州,東南臨海,與臺灣隔峽相望。島邊有山,曰玉璽山,此山林木清秀,靈氣十足。閑來登高,也是美事。 小島...
    石竹閱讀 307評論 35 10