最近在处理一些问题,项目里有录制视频和定位功能,在第一次用户判断是否开启权限时,用户选择不开启,之后用户在项目里点击到涉及相机和定位的相关功能,需要添加的一些提示响应.
1.相机
首先判断用户是否开启照相机权限
NSString* mediaType =AVMediaTypeVideo;
AVAuthorizationStatusauthorizationStatus = [AVCaptureDeviceauthorizationStatusForMediaType:mediaType];
if(authorizationStatus ==AVAuthorizationStatusRestricted|| authorizationStatus ==AVAuthorizationStatusDenied) {
UIAlertView*alertView=[[UIAlertViewalloc]initWithTitle:@"“红人装”想访问你的相机"message:@"请在系统设置中开启相机服务(设置>红人装>相机>开启)"delegate:selfcancelButtonTitle:@"取消"otherButtonTitles:@"去设置",nil];
alertView.delegate=self;
alertView.tag=1;
[alertViewshow];
//用户未开启权限
}else{
//用户开启权限
}
2.麦克风
首先判断用户是否开启麦克风权限
AVAuthorizationStatusauthStatus = [AVCaptureDeviceauthorizationStatusForMediaType:AVMediaTypeAudio];
switch(authStatus) {
caseAVAuthorizationStatusNotDetermined:
//没有询问是否开启麦克风
_flag=1;
break;
caseAVAuthorizationStatusRestricted:
//未授权,家长限制
_flag=0;
break;
caseAVAuthorizationStatusDenied:
//玩家未授权
_flag=0;
break;
caseAVAuthorizationStatusAuthorized:
//玩家授权
_flag=2;
break;
default:
break;
}
if(_flag==0) {
UIAlertView*alertView=[[UIAlertViewalloc]initWithTitle:@"“红人装”想访问你的麦克风"message:@"请在系统设置中开启麦克风服务(设置>红人装>麦克风>开启)"delegate:selfcancelButtonTitle:@"取消"otherButtonTitles:@"去设置",nil];
alertView.delegate=self;
alertView.tag=2;
[alertViewshow];
//用户未开启麦克风权限
}else{
//用户开启麦克风权限
}
3.定位
首先判断用户是否开启定位权限
if([CLLocationManagerlocationServicesEnabled]) {
//判断用户是否允许程序获取位置权限
if([CLLocationManagerauthorizationStatus]==kCLAuthorizationStatusAuthorizedWhenInUse||[CLLocationManagerauthorizationStatus]==kCLAuthorizationStatusAuthorizedAlways)
{
//用户允许获取位置权限
}else
{
//用户拒绝开启用户权限
UIAlertView*alertView=[[UIAlertViewalloc]initWithTitle:@"打开[定位服务权限]来允许[红人装]确定您的位置"message:@"请在系统设置中开启定位服务(设置>隐私>定位服务>开启)"delegate:selfcancelButtonTitle:@"取消"otherButtonTitles:@"去设置",nil];
alertView.delegate=self;
alertView.tag=4;
[alertViewshow];
}
}else{
UIAlertView*alertView=[[UIAlertViewalloc]initWithTitle:@"打开[定位服务]来允许[红人装]确定您的位置"message:@"请在系统设置中开启定位服务(设置>隐私>定位服务>红人装>始终)"delegate:selfcancelButtonTitle:@"取消"otherButtonTitles:@"去设置",nil];
alertView.delegate=self;
alertView.tag=3;
[alertViewshow];
}
///UIAlertViewDelegate 的代理(这里实现点击弹框的跳转事件)
-(void)alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(alertView.tag==1) {
if(buttonIndex ==1) {
NSURL*url = [NSURLURLWithString:UIApplicationOpenSettingsURLString];
if( [[UIApplicationsharedApplication]canOpenURL:url] ) {
[[UIApplicationsharedApplication]openURL:url];
}
}
}elseif(alertView.tag==2) {
if(buttonIndex ==1) {
NSURL*url = [NSURLURLWithString:UIApplicationOpenSettingsURLString];
if( [[UIApplicationsharedApplication]canOpenURL:url] ) {
[[UIApplicationsharedApplication]openURL:url];
}
}
}
if(alertView.tag==3) {
if(buttonIndex ==1) {
if([[[UIDevicecurrentDevice]systemVersion]floatValue] >=10.000000) {
//跳转到定位权限页面
NSURL*url = [NSURLURLWithString:UIApplicationOpenSettingsURLString];
if( [[UIApplicationsharedApplication]canOpenURL:url] ) {
[[UIApplicationsharedApplication]openURL:url];
}
}else{
//跳转到定位开关界面
NSURL*url = [NSURLURLWithString:@"prefs:root=LOCATION_SERVICES"];
if( [[UIApplicationsharedApplication]canOpenURL:url] ) {
[[UIApplicationsharedApplication]openURL:url];
}
}
}
}elseif(alertView.tag==4) {
if(buttonIndex ==1) {
//跳转到定位权限页面
NSURL*url = [NSURLURLWithString:UIApplicationOpenSettingsURLString];
if( [[UIApplicationsharedApplication]canOpenURL:url] ) {
[[UIApplicationsharedApplication]openURL:url];
}
}
}
}
//主要想法就是首先判断用户是否开启权限 如果没有开启 就弹出一个弹框提示, 两个按钮 一个取消 一个去设置,然后给每一个弹框不同的tag值做区别,以便在在UIAlertViewDelegate的代理方法里根据不同的tag值,确定不同的弹框,并给两个按钮相应的点击事件,如果用户点击取消按钮没有不响应,点击去设置则跳转到手机的设置的相应位置.