iOS开发中有时候有这样的需求:当用户设置不允许访问照片、麦克风和相机等系统权限的时候,这时需要直接跳转到系统的隐私界面进行设置。

判断是否开启权限

前面已经说过,我们需要在用户不允许访问的时候跳转,那么首先我们就要判断一些是否已经开启系统相机权限了。

照片权限检测

需要:#import <AssetsLibrary/AssetsLibrary.h> //导入此类和AssetsLibrary.framework框架

代码如下:

int author = [ALAssetsLibrary authorizationStatus];
NSLog(@"author type:%d",author);
if(author == ALAuthorizationStatusRestricted || author == ALAuthorizationStatusDenied) {
// The user has explicitly denied permission for media capture.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"无法使用相册"
message:@"请在iPhone的\"设置-隐私-照片\"中允许访问照片。"
delegate:self
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
return;

ALAssetsLibrary类

ALAssetsLibrary类是代表系统中整个资源库,使用它可以访问资源库中的资源和保存照片,视频等功能。

    //判断当前应用是否能访问相册资源
/*
typedef NS_ENUM(NSInteger, ALAuthorizationStatus) {
ALAuthorizationStatusNotDetermined = 0, 用户尚未做出了选择这个应用程序的问候
ALAuthorizationStatusRestricted, 此应用程序没有被授权访问的照片数据。可能是家长控制权限。
ALAuthorizationStatusDenied, 用户已经明确否认了这一照片数据的应用程序访问.
ALAuthorizationStatusAuthorized 用户已授权应用访问照片数据.
}
*/

访问摄像头

需要:#import <AVFoundation/AVFoundation.h>

代码如下:

if(isIOS7AndLater) {

NSString *mediaType = AVMediaTypeVideo;// Or AVMediaTypeAudio
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
NSLog(@"---cui--authStatus--------%d",authStatus);
// This status is normally not visible—the AVCaptureDevice class methods for discovering devices do not return devices the user is restricted from accessing.
if(authStatus ==AVAuthorizationStatusRestricted){
NSLog(@"Restricted");
}else if(authStatus == AVAuthorizationStatusDenied){
// The user has explicitly denied permission for media capture.
NSLog(@"Denied"); //应该是这个,如果不允许的话
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
message:@"请在设备的\"设置-隐私-相机\"中允许访问相机。"
delegate:self
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
return;
}
else if(authStatus == AVAuthorizationStatusAuthorized){//允许访问
// The user has explicitly granted permission for media capture, or explicit user permission is not necessary for the media type in question.
NSLog(@"Authorized"); }else if(authStatus == AVAuthorizationStatusNotDetermined){
// Explicit user permission is required for media capture, but the user has not yet granted or denied such permission.
[AVCaptureDevice requestAccessForMediaType:mediaType completionHandler:^(BOOL granted) {
if(granted){//点击允许访问时调用
//用户明确许可与否,媒体需要捕获,但用户尚未授予或拒绝许可。
NSLog(@"Granted access to %@", mediaType);
}
else {
NSLog(@"Not granted access to %@", mediaType);
} }];
}else {
NSLog(@"Unknown authorization status");
}
}

麦克风权限检测

代码如下:

  //检测麦克风功能是否打开
[[AVAudioSessionsharedInstance]requestRecordPermission:^(BOOL granted) {
if (!granted)
{
[ViewUtilalertViewWithString:NSLocalizedString(@"麦克风功能未开启",nil)];
}
else
{ [selfrecord:sender];
}
}];

如何跳转到系统设置界面

判断权限是否设置之后就可以在相应的代理方法进行界面跳转了,那么如何进行跳转呢?

首先要在项目中的info.plist中添加 URL types 并设置一项URL Schemes为prefs,如下图:

1.jpg

实现代码如下:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];//url为具体路径

以下是跳转到一些常用界面的代码

隐私->照片界面

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=Privacy&path=PHOTOS"]];

隐私->相机界面

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=Privacy&path=CAMERA"]];

蓝牙设置界面

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=Bluetooth"]];

其他界面参数配置

About — prefs:root=General&path=About

Accessibility — prefs:root=General&path=ACCESSIBILITY

Airplane Mode On — prefs:root=AIRPLANE_MODE

Auto-Lock — prefs:root=General&path=AUTOLOCK

Brightness — prefs:root=Brightness

Bluetooth — prefs:root=General&path=Bluetooth

Date & Time — prefs:root=General&path=DATE_AND_TIME

FaceTime — prefs:root=FACETIME

General — prefs:root=General

Keyboard — prefs:root=General&path=Keyboard

iCloud — prefs:root=CASTLE

iCloud Storage & Backup — prefs:root=CASTLE&path=STORAGE_AND_BACKUP

International — prefs:root=General&path=INTERNATIONAL

Location Services — prefs:root=LOCATION_SERVICES

Music — prefs:root=MUSIC

Music Equalizer — prefs:root=MUSIC&path=EQ

Music Volume Limit — prefs:root=MUSIC&path=VolumeLimit

Network — prefs:root=General&path=Network

Nike + iPod — prefs:root=NIKE_PLUS_IPOD

Notes — prefs:root=NOTES

Notification — prefs:root=NOTIFICATIONS_ID

//

@"prefs:root=NOTIFICATIONS_ID&path=应用的boundleId"

Phone — prefs:root=Phone

Photos — prefs:root=Photos

Profile — prefs:root=General&path=ManagedConfigurationList

Reset — prefs:root=General&path=Reset

Safari — prefs:root=Safari

Siri — prefs:root=General&path=Assistant

Sounds — prefs:root=Sounds

Software Update — prefs:root=General&path=SOFTWARE_UPDATE_LINK

Store — prefs:root=STORE

Twitter — prefs:root=TWITTER

Usage — prefs:root=General&path=USAGE

VPN — prefs:root=General&path=Network/VPN

Wallpaper — prefs:root=Wallpaper

Wi-Fi — prefs:root=WIFI

文/小球hy(简书作者)
原文链接:http://www.jianshu.com/p/1fb3f60b689a
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。

iOS之访问权限以及跳转到系统界面的更多相关文章

  1. iOS 权限判断 跳转对应设置界面

    相机权限 1.1 使用说明 在合适的地方导入#import <AVFoundation/AVFoundation.h> 使用AVAuthorizationStatus类获取当前权限状态 在 ...

  2. Android系统修改硬件设备访问权限

    Android系统修改硬件设备访问权限 在硬件抽象层模块文件(so)文件中,提供的函数调用open函数来打开设备文件,比如/dev/gpio,如果不修改设备文件/dev/gpio的访问权限,那么应用程 ...

  3. ios 判断用户是否开启权限---并跳转设置

    ios 判断用户是否开启权限---并跳转设置 ios 判断用户是否开启权限---并跳转“系统设置” 1.判断 访问相册 或 相机 权限是否开启 2.检测是否开启定位 后面将持续更新 只有在应用请求过位 ...

  4. Swift - 判断是否有某功能访问权限,没有则提示,并自动跳转到设置页

    由于 iOS 系统的安全限制,App 如果需要访问设备的通讯录.麦克风. 相册. 相机.地理位置等时,需要请求用户是否允许访问.   有时用户不小心点了“不允许”,后面可能就不知道要去哪里再开启这个权 ...

  5. iOS重写和成员变量访问权限

    一.重写机制 1.覆盖父类的方法 2.对父类方法做进一步的补充 注意:父类声明过得方法,子类无需声明. * 子类如果重写了父类的方法: 1.父类的指针指向子类的对象,则调用方法时,调用的是子类的方法: ...

  6. iOS判断一些权限是否被禁止

    iOS中经常会遇到访问相册.相机.麦克疯.蓝牙.以及推送等权限,所以每次我们要使用这些权限是都要记得查看用户是否允许了,如果用户禁止了你的访问权限,你仍然去调取相册或者相机等,那么就会先出现下面的这个 ...

  7. ios 10 访问设置问题

    ios 10 访问设置问题 从ios8之api支持访问设置通过访问UIApplicationOpenSettingsURLString来跳转设置 NSURL*url=[NSURL URLWithStr ...

  8. Xcode添加摄像机访问权限<转>

    转帖地址:http://www.manew.com/thread-97708-1-1.html ============================================== ios系统 ...

  9. angularjs中的页面访问权限设置

    11月在赶一个项目,这阵子比较忙,挤挤时间更一篇博客吧,如标题所述说说在ng中页面访问权限控制的问题,水平有限各位看官见谅: 在以往的项目中,前后端常见的配合方式是前端提供页面和ui加一点DuangD ...

随机推荐

  1. 基础拾遗------redis详解

    基础拾遗 基础拾遗------特性详解 基础拾遗------webservice详解 基础拾遗------redis详解 基础拾遗------反射详解 基础拾遗------委托详解 基础拾遗----- ...

  2. PostgreSQL杀掉死锁的链接

    查到对应的用户的活动连接: select * from pg_stat_activity where username="xxx"; 杀掉死锁的连接: select pg_term ...

  3. jQuery 获取 radio 选中后的文字

    如果html为 <input type="radio" id="test" name="test" value="1&quo ...

  4. CSS3:radial-gradient,径向渐变的使用方法

    语法 径向渐变不同于线性渐变,线性渐变是从“一个方向”向“另一个方向”的颜色渐变,而径向渐变是从“一个点”向四周的颜色渐变.其语法如下: background: radial-gradient(cen ...

  5. rsync同步架构

    1.1 rsync服务器端配置 1.1.1 查看服务器端rsync版本 1.1.2 创建配置文件 默认安装好rsync程序后,并不会自动创建rsync的主配置文件,需要手工来创建,其主配置文件为“/e ...

  6. [Unity3D]巧妙利用父级子级实现Camera场景平面漫游

    本文系作者原创,转载请注明出处 入门级的笔者想了一上午才搞懂那个欧拉角的Camera旋转..=.= 在调试场景的时候,每次都本能的按下W想前进,但是这是不可能的(呵呵) 于是便心血来潮想顺便添加个Ke ...

  7. kettle中参数和变量的区别

    图一: 图二: 何时使用'?'何事使用${}应当根据情况: 在图二中使用的是${}因为此时没有"作为参数的字段",所以只能用el表达式直接获取其值,在图一中有"作为参数的 ...

  8. [No0000A4]DOS命令(cmd)批处理:替换字符串、截取字符串、扩充字符串、获取字符串长度

    1.替换字符串,即将某一字符串中的特定字符或字符串替换为给定的字符串.举例说明其功能:========================================= @echo off set a ...

  9. [No00008B]远程桌面发送“Ctrl+Alt+Delete”组合键调用任务管理器

    向远程桌面发送"Ctrl+Alt+Delete"组合键的两种方法 1.在本地按下Ctrl+Alt+End,可以成功发送"Ctrl+Alt+Delete"组合键! ...

  10. [LeetCode] Combination Sum 组合之和

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...