前言:最近做的IM项目测试那边有要求提高用户体验,添加相机等授权设置,用以前的方法发现实现不了了,上网查了一下,发现IOS10以后方法改变了,在此记录一下。
一、权限判断及代码实现跳转
1、AppDelegate里定义
-(void)alertCamera:(NSString *)messageStr{
UIAlertView *alertCamera = [[UIAlertView alloc] initWithTitle:@"相机功能不可用" message:messageStr delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"授权", nil];
[alertCamera show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex==0) {
}else{
[self Adjusting_system_settings];
}
}
//跳转系统权限设置( iOS10之后不允许跳转到设置的子页面,只允许跳转到设置界面)
-(void)Adjusting_system_settings{
if (IOS10) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
} else {
NSURL *url = [NSURL URLWithString:@"prefs:root=Privacy&&path=CAMERA"];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
}
}
2、在用到的类导入头文件#import <AVFoundation/AVFoundation.h>,.m里调用方法
//权限判断
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied) {
[AppDelegate alertCamera:@"无权限,请先在设置中为该软件授权相机服务,然后重试!"];
return ;
}else{
}