iOS相机、麦克风等权限的判断与设置
来源:互联网 发布:php 获取客户端信息 编辑:程序博客网 时间:2024/04/25 07:04
一、iOS应用权限检测
在涉及到这个问题的时候,首先为了适配iOS10系统,我们必须首先在info.plist文件中声明将要用到的权限,否则将会引起崩溃如下:
“This app has crashed because it attempted to access privacy-sensitive data without a usage description. The app’s Info.plist must contain an NSMicrophoneUsageDescription key with a string value explaining to the user how the app uses this data.”
那么设置权限声明的的方式如下:
我们需要点击Info.plist中加号,增加需要授权key值并填写相应的权限使用声明。
1.相机与麦克风
检测相机与麦克风权限需要导入AVFoundataion框架
#import <AVFoundation/AVFoundation.h>
/**//相机、麦克风的授权状态typedef NS_ENUM(NSInteger, AVAuthorizationStatus) {AVAuthorizationStatusNotDetermined = 0,//未询问过用户是否授权AVAuthorizationStatusRestricted, //未授权,例如家长控制AVAuthorizationStatusDenied, //未授权,用户曾选择过拒绝授权AVAuthorizationStatusAuthorized //已经授权} NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;*/
/** 检测相机的方法 @param permissionGranted 相机授权成功执行的方法 @param noPermission 相机授权失败或者未授权执行的方法 */+ (void)checkCameraAuthorizationGrand:(void (^)())permissionGranted withNoPermission:(void (^)())noPermission{ AVAuthorizationStatus videoAuthStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]; switch (videoAuthStatus) { case AVAuthorizationStatusNotDetermined: { //第一次提示用户授权 [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) { granted ? permissionGranted() : noPermission(); }]; break; } case AVAuthorizationStatusAuthorized: { //通过授权 permissionGranted(); break; } case AVAuthorizationStatusRestricted: //不能授权 NSLog(@"不能完成授权,可能开启了访问限制"); case AVAuthorizationStatusDenied:{ //提示跳转到相机设置(这里使用了blockits的弹窗方法) UIAlertView *alert = [UIAlertView bk_showAlertViewWithTitle:@"相机授权" message:@"跳转相机授权设置" cancelButtonTitle:@"取消" otherButtonTitles:@[@"设置"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) { if (buttonIndex == 1) { //请求授权 [self requetSettingForVideo]; } }]; [alert show]; } break; default: break; }}
2.相册
这里针对于iOS8及其以后的系统相册检测方法,使用到的PHPhotoLibrary需要导入Photos框架。
#import <Photos/Photos.h>
//相册的授权状态typedef NS_ENUM(NSInteger, PHAuthorizationStatus) { PHAuthorizationStatusNotDetermined = 0, // User has not yet made a choice with regards to this application PHAuthorizationStatusRestricted, // This application is not authorized to access photo data. // The user cannot change this application’s status, possibly due to active restrictions // such as parental controls being in place. PHAuthorizationStatusDenied, // User has explicitly denied this application access to photos data. PHAuthorizationStatusAuthorized // User has authorized this application to access photos data.} PHOTOS_AVAILABLE_IOS_TVOS(8_0, 10_0);
/** 检测访问相册的权限 这里的方法适用于iOS8及其以后版本 @param permissionGranted 相册授权成功执行的方法 @param noPermission 相册授权失败或者未授权执行的方法 */+ (void)checkPhotoAlbumAuthorizationGrand:(void (^)())permissionGranted withNoPermission:(void (^)())noPermission{ PHAuthorizationStatus photoAuthStatus = [PHPhotoLibrary authorizationStatus]; switch (photoAuthStatus) { case PHAuthorizationStatusNotDetermined: { //第一次提示用户授权 [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) { status == PHAuthorizationStatusAuthorized ? permissionGranted() : noPermission(); }]; break; } case PHAuthorizationStatusAuthorized: { //已经通过授权 permissionGranted(); break; } case PHAuthorizationStatusRestricted: //不能授权 NSLog(@"不能完成授权,可能开启了访问限制"); case PHAuthorizationStatusDenied:{ //提示跳转相册授权设置 UIAlertView *alert = [UIAlertView bk_showAlertViewWithTitle:@"相册授权" message:@"跳转相册授权设置" cancelButtonTitle:@"取消" otherButtonTitles:@[@"设置"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) { if (buttonIndex == 1) { [self requetSettingForPhotoAlbum]; } }]; [alert show]; break; } default: break; }}
二、iOS应用跳转权限设置
在iOS8以后的系统中,跳转设置使用如下方法:
+ (void)requetSettingForAuth{ NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; if ([ [UIApplication sharedApplication] canOpenURL:url]) { [[UIApplication sharedApplication] openURL:url]; }}
0 0
- iOS相机、麦克风等权限的判断与设置
- ios判断相册,相机,麦克风等权限
- iOS 相机 麦克风 照片 权限的检测与获取
- iOS10适配——相机,通讯录,麦克风等权限设置
- iOS相机 相册 麦克风 定位等权限是否拥有
- iOS 解决iOS10相册、相机、麦克风等权限使用问题
- xcode8 iOS10上关于相册,相机,麦克风 等访问权限plist设置的问题
- iOS中获取相册、相机、定位、以及麦克风权限设置
- iOS相册权限、相机权限、麦克风权限
- iOS相册权限、相机权限、麦克风权限
- iOS获取系统相册、相机、麦克风权限的工具
- iOS之访问(隐私)权限设置 通讯录、相册、麦克风等
- ios获取相机,相册,麦克风权限
- iOS 10诸如相机、相册、通讯录、麦克风、定位权限设置,防止奔溃或上架被拒
- ios 获取相机权限 判断相机状态
- 关于部分国产手机的相机录音等权限判断
- iOS10.0对用户的隐私权限越来越重视,要想正常访问相册,相机,位置,麦克风,蓝牙,健康等
- iOS 判断应用是否有使用相机的权限
- 2017年1月10号
- 架构师的必备素质和成长途径
- 第三章 数组和字符串(1)
- Servlet
- 江西省西山学校荣获“中国民办十大知名品牌学校 ”刘在远校长荣获“中国民办教育功勋人物”
- iOS相机、麦克风等权限的判断与设置
- java向上转型和向下转型
- input输入框file类型选择同样输入框第二次不触发onchange事件的解决办法
- CSS3rem自适应
- 更改tomcat端口8080为80的时候,有系统端口占用的问题
- 不需要再纠结 JAVA 的效率问题!
- 负载均衡
- 红米note3 Failure [INSTALL_CANCELED_BY_USER]
- 最大和