iOS 相册、通讯录、网络等权限的状态获取,持续更新

联网权限,CoreTelephony.framework框架下的CTCellularData类
  • 获取权限状态
CTCellularData *cellularData = [[CTCellularData alloc]init];
CTCellularDataRestrictedState state = cellularData.restrictedState;
switch (state) {
    case kCTCellularDataRestrictedStateUnknown:
        NSLog(@"未知");
        break;
    case kCTCellularDataRestricted:
        NSLog(@"受限制,无权限");
        break;
    case kCTCellularDataNotRestricted:
        NSLog(@"不被限制");
        break;
}
  • 请求授权
CTCellularData *cellularData = [[CTCellularData alloc]init];
cellularData.cellularDataRestrictionDidUpdateNotifier = ^(CTCellularDataRestrictedState state){
    switch (state) {
        case kCTCellularDataRestrictedStateUnknown:
            NSLog(@"未知");
            break;
        case kCTCellularDataRestricted:
            NSLog(@"受限制,无权限");
            break;
        case kCTCellularDataNotRestricted:
            NSLog(@"不被限制");
            break;
    };
};
相册权限,Photos.framework
  • 获取权限状态
PHAuthorizationStatus photoAuthorStatus = [PHPhotoLibrary authorizationStatus];
switch (photoAuthorStatus) {
    case PHAuthorizationStatusNotDetermined:
        NSLog(@"尚未授权");
        break;
    case PHAuthorizationStatusRestricted:
        NSLog(@"受限制,无权限");
        break;
    case PHAuthorizationStatusDenied:
        NSLog(@"用户拒绝授权");
        break;
    case PHAuthorizationStatusAuthorized:
        NSLog(@"用户已授权");
        break;
}
  • 请求授权
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
    switch (status) {
        case PHAuthorizationStatusNotDetermined:
            NSLog(@"尚未授权");
            break;
        case PHAuthorizationStatusRestricted:
            NSLog(@"受限制,无权限");
            break;
        case PHAuthorizationStatusDenied:
            NSLog(@"用户拒绝授权");
            break;
        case PHAuthorizationStatusAuthorized:
            NSLog(@"用户已授权");
            break;
    }
}];
相机权限,AVFoundation.framework
  • 获取权限状态
AVAuthorizationStatus AVstatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
switch (AVstatus) {
    case AVAuthorizationStatusNotDetermined:
        NSLog(@"尚未授权");
        break;
    case AVAuthorizationStatusRestricted:
        NSLog(@"受限制,无权限");
        break;
    case AVAuthorizationStatusDenied:
        NSLog(@"用户拒绝授权");
        break;
    case AVAuthorizationStatusAuthorized:
        NSLog(@"用户已授权");
        break;
}
  • 请求授权
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
    if (granted) {
        NSLog(@"已授权");
    } else {
        NSLog(@"未授权");
    }
}];
麦克风权限,AVFoundation.framework
  • 获取授权状态
AVAuthorizationStatus AVstatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];//麦克风权限
switch (AVstatus) {
    case AVAuthorizationStatusNotDetermined:
        NSLog(@"尚未授权");
        break;
    case AVAuthorizationStatusRestricted:
        NSLog(@"受限制,无权限");
        break;
    case AVAuthorizationStatusDenied:
        NSLog(@"用户拒绝授权");
        break;
    case AVAuthorizationStatusAuthorized:
        NSLog(@"用户已授权");
        break;
}
  • 请求授权
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
    if (granted) {
        NSLog(@"已授权");
    } else {
        NSLog(@"未授权");
    }
}];
通讯录权限,Contacts.framework,AddressBook.framework
  • 获取授权状态
#ifdef __IPHONE_9_0

CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
switch (status) {
    case CNAuthorizationStatusNotDetermined:
        NSLog(@"尚未授权");
        break;
    case CNAuthorizationStatusRestricted:
        NSLog(@"受限制,无权限");
        break;

    case CNAuthorizationStatusDenied:
        NSLog(@"用户拒绝授权");
        break;
    case CNAuthorizationStatusAuthorized:
        NSLog(@"用户已授权");
        break;
}

#else

ABAuthorizationStatus ABstatus = ABAddressBookGetAuthorizationStatus();
switch (ABstatus) {
    case kABAuthorizationStatusNotDetermined:
        NSLog(@"尚未授权");
        break;
    case kABAuthorizationStatusRestricted:
        NSLog(@"受限制,无权限");
        break;
    case kABAuthorizationStatusDenied:
        NSLog(@"用户拒绝授权");
        break;
    case kABAuthorizationStatusAuthorized:
        NSLog(@"用户已授权");
        break;
}

#endif
  • 请求授权
#ifdef __IPHONE_9_0

CNContactStore *contactStore = [[CNContactStore alloc] init];
[contactStore requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
    if (granted) {
        NSLog(@"已授权");
    } else {
        NSLog(@"未授权");
    }
}];

#else

ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
    if (granted) {
        NSLog(@"已授权");
    } else {
        NSLog(@"未授权");
    }
});

#endif
定位权限,CoreLocation.framework
  • 获取授权状态
if ([CLLocationManager locationServicesEnabled]) {
    
    CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
    switch (status) {
        case kCLAuthorizationStatusNotDetermined:
            NSLog(@"尚未授权");
            break;
        case kCLAuthorizationStatusRestricted:
            NSLog(@"受限制,无权限");
            break;
        case kCLAuthorizationStatusDenied:
            NSLog(@"用户拒绝授权");
            break;
        case kCLAuthorizationStatusAuthorizedAlways:
            NSLog(@"用户已授权");
            break;
        case kCLAuthorizationStatusAuthorizedWhenInUse:
            NSLog(@"用户已授权");
            break;
            
        default:
            break;
    }
    
}else {
    NSLog(@"定位服务尚未开启");
}
  • 请求授权
CLLocationManager *locationManager = [[CLLocationManager alloc] init;
[locationManager requestAlwaysAuthorization];
//    [locationManager requestWhenInUseAuthorization];

你可能感兴趣的:(iOS 相册、通讯录、网络等权限的状态获取,持续更新)