美文网首页
iOS 相机,通讯录,麦克风等权限详解

iOS 相机,通讯录,麦克风等权限详解

作者: 浅宇落 | 来源:发表于2019-08-16 15:57 被阅读0次

iOS10 以后,需要在plist文件里面添加各种权限:

升到iOS10之后,需要设置权限的有:
麦克风权限:Privacy - Microphone Usage Description 是否允许此App使用你的麦克风?
相机权限: Privacy - Camera Usage Description 是否允许此App使用你的相机?
相册权限: Privacy - Photo Library Usage Description 是否允许此App访问你的媒体资料库?
通讯录权限: Privacy - Contacts Usage Description 是否允许此App访问你的通讯录?
蓝牙权限:Privacy - Bluetooth Peripheral Usage Description 是否许允此App使用蓝牙?
语音转文字权限:Privacy - Speech Recognition Usage Description 是否允许此App使用语音识别?
日历权限:Privacy - Calendars Usage Description 
定位权限:Privacy - Location When In Use Usage Description 
定位权限: Privacy - Location Always Usage Description 
位置权限:Privacy - Location Usage Description
媒体库权限:Privacy - Media Library Usage Description
健康分享权限:Privacy - Health Share Usage Description
健康更新权限:Privacy - Health Update Usage Description
运动使用权限:Privacy - Motion Usage Description
音乐权限:Privacy - Music Usage Description
提醒使用权限:Privacy - Reminders Usage Description
Siri使用权限:Privacy - Siri Usage Description
电视供应商使用权限:Privacy - TV Provider Usage Description
视频用户账号使用权限:Privacy - Video Subscriber Account Usage Description

1.麦克风权限

导入#import <AVFoundation/AVFoundation.h>

//麦克风权限
AVAuthorizationStatus micStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
if (micStatus != AVAuthorizationStatusAuthorized) { // 麦克风权限没有开启
  [self dealMicAuthWithStatus:micStatus];
}else {
  NSLog(@"麦克风权限已经开启");
}
- (void)dealMicAuthWithStatus:(AVAuthorizationStatus)status {
    switch (status) {
        case AVAuthorizationStatusAuthorized:
            NSLog(@"Authorized");//已授权
            break;
        case AVAuthorizationStatusDenied://拒绝
        case AVAuthorizationStatusRestricted: { //家长限制
            // 提示框提示
            [self showAlertWithMessage:@"您已关闭麦克风权限,请前往设置打开"];
        }
            break;
        case AVAuthorizationStatusNotDetermined: {
            [AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
                if (granted) {
                    NSLog(@"允许麦克风权限");
                }else {
                    NSLog(@"拒绝麦克风权限");
                }
            }];
        }
            break;
        default:
            break;
    }
}
#pragma mark - 提示框提示
- (void)showAlertWithMessage:(NSString *)message {
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"项目名称提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
        if([[UIApplication sharedApplication] openURL:url]) {
            [[UIApplication sharedApplication] openURL:url];
        }
    }];
    [alertVC addAction:cancleAction];
    [alertVC addAction:sureAction];
    
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertVC animated:YES completion:nil];
}
系统提示.png 提示框提示.png

2.相机权限

//相机权限
AVAuthorizationStatus videoStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (videoStatus != AVAuthorizationStatusAuthorized) { // 相机权限没有开启
  [self dealVideoAuthWithStatus:videoStatus];
}else {
  NSLog(@"相机权限已经开启");
}
- (void)dealVideoAuthWithStatus:(AVAuthorizationStatus)status {
    switch (status) {
        case AVAuthorizationStatusAuthorized:
            NSLog(@"Authorized");//已授权
            break;
        case AVAuthorizationStatusDenied://拒绝
        case AVAuthorizationStatusRestricted: { //家长限制
            // 提示框提示
            [self showAlertWithMessage:@"您已关闭相机权限,请前往设置打开"];
        }
            break;
        case AVAuthorizationStatusNotDetermined: {
            [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
                if (granted) {
                    NSLog(@"允许相机权限");
                }else {
                    NSLog(@"拒绝相机权限");
                }
            }];
        }
            break;
        default:
            break;
    }
}
系统提示.png
提示框提示.png

3.相册权限

导入#import <Photos/Photos.h>

// 相册权限
PHAuthorizationStatus photoStatus = [PHPhotoLibrary authorizationStatus];
if (photoStatus != PHAuthorizationStatusAuthorized) { // 相册权限没有开启
  [self dealPhotoAuthWithStatus:photoStatus];
}else {
  NSLog(@"相册权限已经开启");
}
- (void)dealPhotoAuthWithStatus:(PHAuthorizationStatus)status {
    switch (status) {
        case PHAuthorizationStatusAuthorized:
            NSLog(@"Authorized");//已授权
            break;
        case PHAuthorizationStatusDenied://拒绝
        case PHAuthorizationStatusRestricted: { //家长限制
            // 提示框提示
            [self showAlertWithMessage:@"您已关闭相册权限,请前往设置打开"];
        }
            break;
        case PHAuthorizationStatusNotDetermined: {
            [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
                if (status == PHAuthorizationStatusAuthorized) {
                    NSLog(@"相机权限开启");
                }else {
                    NSLog(@"相机权限关闭");
                }
            }];
        }
            break;
        default:
            break;
    }
}
系统提示.png
提示框提示.png

相关文章

  • iOS10 Xcode里面Info.plist文件权限问题

    参考文档: iOS10适配——相机,通讯录,麦克风等权限设置 Xcode中的Info.plist字段列表详解

  • APP基础功能的配置管理之系统权限控制

    开篇:在iOS系统上,对于系统权限、用户权限的审核是非常严格的。比如相机、相册、麦克风、通讯录、定位和蓝牙等,在a...

  • iOS系统权限管理

    iOS系统权限管理主要有以下权限:网络、相册、推送、相机以及麦克风、通讯录、定位;https://www.jian...

  • iOS 相机,通讯录,麦克风等权限详解

    iOS10 以后,需要在plist文件里面添加各种权限: 1.麦克风权限 导入#import 2.相机权限 3.相...

  • iOS10更新的坑

    相册,相机,麦克风等使用会崩溃。iOS10的新特性,以后使用相机,相册,麦克风,通讯录都要在Info.plist文...

  • iPhone系统权限

    需要引用到的头文件有 相册权限 照相机权限 麦克风权限 通知权限 定位权限 通讯录权限

  • iOS开发之权限崩溃问题 (iOS10)

    Xcode项目在iOS10系统上运行,需要涉及到相机/通讯录/麦克风等权限的开启,一般在log日志都有输出,有两种...

  • ios开发中的权限

    权限分类 联网权限 相册权限 相机 麦克风权限 定位权限 推送权限 通讯录权限 日历 备忘录权限 联网权限 引入头...

  • iOS 相关权限

    权限分类 联网权限 相册权限 相机、麦克风权限 定位权限 推送权限 通讯录权限 日历、备忘录权限 1.联网权限 引...

  • ios 系统权限设置

    iOS10配置须知 在iOS10中,如果你的App想要访问用户的相机、相册、麦克风、通讯录等等权限,都需要进行相关...

网友评论

      本文标题:iOS 相机,通讯录,麦克风等权限详解

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