zoukankan      html  css  js  c++  java
  • iOS:检测多媒体(相机、相册、麦克风)设备权限,弹框提示

    一、感言

    新年伊始,万象更新,一转眼,就2019年了。

    作为一个科班生,从事移动端开发好几年了,回顾曾经的摸爬滚打,兢兢业业,严格的来说,多少算是入行了。

    过去成绩如何暂且不说,新的一年,我当加倍努力,凤凰涅槃,浴火重生。

    二、介绍

    在项目中,多媒体的使用非常常见,那么,询问设备的权限必不可少。

    优点:这么做极大的增强了用户体验,友好地告知用户去开启权限。

    例如:相机的使用权限、相册的使用权限、麦克风的使用权限等

    三、代码

    1、相机的使用权限 (导入 #import <AVFoundation/AVFoundation.h>

    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
         [self showAlertMessage:@"该设备不支持相机功能"];
         return;
    }
    
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied){
         [self showAlertMessage:@"应用相机权限受限,请在设置中启用"];
         return;
     }
    
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    picker.delegate = self;
    [self presentViewController:picker animated:YES completion:nil];

    2、相册的使用权限(导入  #import<Photos/PHPhotoLibrary.h>

    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
         [self showAlertMessage:@"该设备不支持相册功能~"];
         return;
    }
    
    PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
    if (status == PHAuthorizationStatusRestricted || status == PHAuthorizationStatusDenied){
        [self showAlertMessage:@"应用相册权限受限,请在设置中启用"];
        return;
    }
        
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    [self presentViewController:picker animated:YES completion:nil];

    3、麦克风的使用权限(导入 #import <AVFoundation/AVFoundation.h>)

    AVAuthorizationStatus audioAuthStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
        
    if (audioAuthStatus == AVAuthorizationStatusNotDetermined) {  //未询问用户是否授权
        //第一次询问用户是否进行授权,只会调用一次
        [AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
           if (!granted) {
              dispatch_async(dispatch_get_main_queue(), ^{ //手动禁止了授权
                 [self showAlertMessage:@"您已禁用了麦克风,请到设置中开启后重试~"];
              });
            }
        }];
     }
     else if (audioAuthStatus == AVAuthorizationStatusAuthorized) { //麦克风已开启
         [[RecordUtility shareRecordUtility] startRecord]; //开始录音
     }
     else{ //未授权
         [self showAlertMessage:@"您未开启麦克风权限,请到设置中开启后重试~"];
     }

     注意:跳转到设置中开启权限(CCBlcokAlertView是继承自UIAlertView的alertView,自己自定义一下吧,简单)

    -(void)showAlertMessage:(NSString *)message{
        CCBlockAlertView *alertView = [[CCBlockAlertView alloc] initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        alertView.delegate = alertView;
        alertView.blockAlertViewDidDismissWithButtonIndex = ^(CCBlockAlertView *blockAlertView, int buttonIndex) {
            if (buttonIndex == 1) { // 点击了去设置,跳转设置
                NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
                if ([[UIApplication sharedApplication] canOpenURL:url]) {
                    [[UIApplication sharedApplication] openURL:url];
                }
            }
        };
        [alertView show];
    }
  • 相关阅读:
    Nginx进阶使用-负载均衡原理及配置实例
    代理服务技术-正向代理、方向代理、透明代理简析
    Docker入门教程-Linux环境安装Nginx及入门使用
    Mybatis进阶使用-一级缓存与二级缓存
    结对第2次作业——WordCount进阶需求
    团队展示
    原型设计(顶会热词统计)
    C++读取文件统计单词个数及频率
    软工实践第一次作业
    课程作业八
  • 原文地址:https://www.cnblogs.com/XYQ-208910/p/10209889.html
Copyright © 2011-2022 走看看