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 == ) { // 点击了去设置,跳转设置
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
}
};
[alertView show];
}
iOS:检测多媒体(相机、相册、麦克风)设备权限,弹框提示的更多相关文章
- Appium 之处理首次启动手机App时的系统权限弹框
一般首次启动一个手机App时都会有系统权限弹框,如下图所示: 权限弹窗上面的按钮都是固定的,只需要定位到“ALLOW”按钮,点击就可以了,代码如下: 这里主要用selenium里面的显示等待模块(We ...
- 解决关闭app权限弹框后无法识别页面对象问题
在使用appium进行安卓端app的自动化测试,我碰到这样下面这几个问题: 1.每次启动我的待测app时总会提示app权限 2.关闭完权限后,无法识别页面对象 第一个问题的解决,我更换不同的真机进行测 ...
- 问题二:appium 搞定权限弹框的一个小办法
public void permission() { for (int i=0; i <= 10; i++) { if (getPageSource().contains("允许&qu ...
- iOS:定制自适应大小的透明吐司弹框
一.简单介绍 创建一个吐司消息的黑色透明弹框,可以根据消息长短自适应大小. 可以手动创建手动显示手动关闭,也可以手动创建自动显示自动关闭. 简单好用. 二.代码使用 .h文件 // // LiveHU ...
- iOS 15 无法弹出授权弹框之解决方案---Your app uses the AppTrackingTransparency framework, but we are unable to locate the App Tracking Transparency permission request when reviewed on iOS 15.0
2021年9月30日下午:我正愉快的期盼着即将到来的国庆假期,时不时刷新下appstoreconnect的网址,28号就提上去的包,今天还在审核中....由于这个版本刚升级的xcode系统和新出的iO ...
- ios中的三种弹框《转》
目前为止,已经知道3种IOS弹框: 1.系统弹框-底部弹框 UIActionSheet (1)用法:处理用户非常危险的操作,比如注销系统等 (2)举例: UIActionSheet *sheet = ...
- ios中的三种弹框
目前为止,已经知道3种IOS弹框: 1.系统弹框-底部弹框 UIActionSheet (1)用法:处理用户非常危险的操作,比如注销系统等 (2)举例: UIActionSheet *sheet = ...
- iOS 10 设备权限问题(相机,相册等)
iOS 10 的设备权限 需要添加的字段有: <!-- 相册 --> <key>NSPhotoLibraryUsageDescription</key> <s ...
- iOS10 相机相册等权限的使用、检测并引导用户开启权限
<!-- 相册 --> <key>NSPhotoLibraryUsageDescription</key> <string>App需要您的同意,才能访问 ...
随机推荐
- SQLALCHEMY_TRACK_MODIFICATIONS adds significant异常的解决方法
- springboot 传List参数
最近项目有个需求,前台需要传list参数请求controller接口,一开始直接使用ResponseBody注解,但实践下来发现参数没有传到controller. 现将处理方式记录如下: 1.前台 ...
- Python学习(二十四)—— 前端基础之Bookstrap
转载自:http://www.cnblogs.com/liwenzhou/p/8214637.html 一.Bootstrap介绍 Bootstrap是Twitter开源的基于HTML.CSS.Jav ...
- 高性能之js
alloyteam团队的总结: 链接在:http://www.alloyteam.com/2012/10/high-performance-front-end-high-performance-jav ...
- List接口相对于Collection接口的特有遍历方法
package com.hxl; import java.util.ArrayList; import java.util.List; public class Test { public stati ...
- P1605 迷宫 dfs回溯法
题目背景 迷宫 [问题描述] 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和 终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在迷宫 中移动有上下 ...
- java定时任务以及Spring使用Quartz调度器执行定时任务
java下的java.util.Timer中类可以实现定时执行任务的执行: 如:让任务立即执行,每隔1s循环执行一次 public class TimerClass { public static v ...
- request和session的区别
request多用于表单提交,表单数据等 session用于服务器端的记录变量,比如用户的登陆和登出啊 request 资源占用小,安全性较高,但是不持续性 session 资源消耗较大,安全性较低, ...
- List集合remove元素的问题
import java.util.*; public class object { public static void main(String[] args) { String str1 = new ...
- CentOS 通过yum在线安装MySQL5.7
CentOS 通过yum在线安装MySQL5.7 Step1: 检测系统是否自带安装mysql # yum list installed | grep mysql Step2: 删除系统自带的mysq ...