iOS相關許可權檢測和申請

weixin_34247155發表於2018-08-06

iOS許可權相關的檢測和申請

在iOS開發過程中常用到的許可權整理如下:

  1. 相簿許可權檢測
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
    switch (status) {
        case PHAuthorizationStatusNotDetermined:
            NSLog(@"使用者還未做出任何選擇");
            break;
        case PHAuthorizationStatusRestricted:
            NSLog(@"許可權收到限制,可能是家長控制許可權");
            break;
        case PHAuthorizationStatusDenied:
            NSLog(@"沒有授權");
            break;
        case PHAuthorizationStatusAuthorized:
            NSLog(@"已經授權");
            break;
        default:
            break;
    } 

相簿許可權申請

[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
        if (status == PHAuthorizationStatusAuthorized) {
            NSLog(@"同意授權");
        }else{
            NSLog(@"未同意或未選擇");
        }
    }];
  1. 相機許可權檢測
AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (status == AVAuthorizationStatusAuthorized) {
        NSLog(@"有許可權");
    }else{
        NSLog(@"沒有");
    }

相機許可權申請

[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
        if (granted) {
            NSLog(@"同意");
        }
    }];
  1. 麥克風
    把上面的AVMediaTypeVideo換成AVMediaTypeAudio即可

  2. 網路許可權
    需要進入標頭檔案#import <CoreTelephony/CTCellularData.h

CTCellularData *cellularData = [CTCellularData new];
    CTCellularDataRestrictedState state = [cellularData restrictedState];
    if (state == kCTCellularDataRestricted) {
        NSLog(@"kCTCellularDataRestricted");
    }else if (state == kCTCellularDataNotRestricted){
        NSLog(@"kCTCellularDataNotRestricted");
    }else{
        NSLog(@"kCTCellularDataRestrictedStateUnknown");
    }
  1. 推送許可權檢測
UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];
    switch (setting.types) {
        case UIUserNotificationTypeNone:
            NSLog(@"在收到通知後,應用程式可能不呈現任何UI。");
            break;
        case UIUserNotificationTypeBadge:
            NSLog(@"應用程式可以在收到通知時標記其圖示。");
            break;
        case UIUserNotificationTypeSound:
            NSLog(@"應用程式可以在接收到通知時播放聲音。");
            break;
        case UIUserNotificationTypeAlert:
            NSLog(@"應用程式可以在接收到通知時顯示警報。");
            break;
        default:
            break;
    }

推送許可權申請

UIUserNotificationSettings *requeatSet = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:requeatSet];
  1. 通訊錄許可權檢測
//ios9之前,檢測
    ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
    if (status == kABAuthorizationStatusAuthorized) {
        NSLog(@"有許可權");
    }else{
        NSLog(@"沒有");
    }

//iOS9及以上,檢測
    CNAuthorizationStatus statu = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
    if (statu == CNAuthorizationStatusAuthorized) {
        NSLog(@"有許可權");
    }else{
        NSLog(@"沒有");
    }

通訊錄許可權申請

//ios9之前,申請
ABAddressBookRef ref = ABAddressBookCreate();
    ABAddressBookRequestAccessWithCompletion(ref, ^(bool granted, CFErrorRef error) {
        if (granted) {
            NSLog(@"同意");
        }
    });

//iOS9及以上,申請
CNContactStore *store = [CNContactStore new];
    [store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
        if (granted) {
            NSLog(@"同意");
        }
    }];
  1. 定位許可權檢測
    引入標頭檔案#import <CoreLocation/CoreLocation.h>
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
    switch (status) {
        case kCLAuthorizationStatusNotDetermined:
            NSLog(@"kCLAuthorizationStatusNotDetermined");
            break;
        case kCLAuthorizationStatusRestricted:
            NSLog(@"kCLAuthorizationStatusRestricted");
            break;
        case kCLAuthorizationStatusDenied:
            NSLog(@"kCLAuthorizationStatusDenied");
            break;
        case kCLAuthorizationStatusAuthorizedAlways:
            NSLog(@"kCLAuthorizationStatusAuthorizedAlways");
            break;
        case kCLAuthorizationStatusAuthorizedWhenInUse:
            NSLog(@"kCLAuthorizationStatusAuthorizedWhenInUse");
            break;
        default:
            break;
    }

定位許可權申請

    CLLocationManager *manager = [CLLocationManager new];
    [manager requestAlwaysAuthorization];
    [manager requestWhenInUseAuthorization];
    manager.delegate = self;

//代理方法中判斷使用者的選擇
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
    switch (status) {
        case kCLAuthorizationStatusNotDetermined:
            NSLog(@"kCLAuthorizationStatusNotDetermined");
            break;
        case kCLAuthorizationStatusRestricted:
            NSLog(@"kCLAuthorizationStatusRestricted");
            break;
        case kCLAuthorizationStatusDenied:
            NSLog(@"kCLAuthorizationStatusDenied");
            break;
        case kCLAuthorizationStatusAuthorizedAlways:
            NSLog(@"kCLAuthorizationStatusAuthorizedAlways");
            break;
        case kCLAuthorizationStatusAuthorizedWhenInUse:
            NSLog(@"kCLAuthorizationStatusAuthorizedWhenInUse");
            break;
        default:
            break;
    }
}

相關文章