美文网首页iOS开发iOS的技术论坛
iOS相关权限检测和申请

iOS相关权限检测和申请

作者: 海上飞鸟 | 来源:发表于2018-08-06 15:38 被阅读0次

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;
    }
}

相关文章

  • iOS相关权限检测和申请

    iOS权限相关的检测和申请 在iOS开发过程中常用到的权限整理如下: 相册权限检测 相册权限申请 相机权限检测 相...

  • 检测 iOS 系统网络权限被关闭

    检测 iOS 系统网络权限被关闭 检测 iOS 系统网络权限被关闭

  • iOS eSIM开发

    iOS eSIM开发步骤 申请eSIMAPI使用权限 配置项目 使用eSIMAPI 一.申请eSIMAPI使用权限...

  • 【转】iOS eSIM开发

    iOS eSIM开发步骤 申请eSIMAPI使用权限 配置项目 使用eSIMAPI 一.申请eSIMAPI使用权限...

  • iOS10配置申请权限

    iOS 10需要配置权限申请。如果没有加权限申请,在运行到需要申请相应权限时的时候程序会崩溃;如果直接直接上传在i...

  • iOS 和 Android ble 对比

    常见问题:1 权限问题 iOS 只需要申请 蓝牙权限即可,而Android还需要申请定位权限2 初始化 都需要判...

  • 权限库升级了~

    关于权限相关的文章已经发了不少:iOS开发中的这些权限,你搞懂了吗?、如何获取iOS应用网络权限?、iOS开发中权...

  • iOS申请权限

    info plist中填写 Privacy - Camera Usage Description //相机权限 P...

  • iOS申请权限

    直接上干货: 喜欢和关注都是对我的鼓励和支持~

  • iOS 权限检测

网友评论

    本文标题:iOS相关权限检测和申请

    本文链接:https://www.haomeiwen.com/subject/yexgvftx.html