ios7开始,用户可以在设置->隐私->中开启或关闭某些系统权限,比如访问相册,相机 ,通讯录,地图,麦克风等。
因此,在我们的程序中,如果要访问系统的某些功能,则最好判断一下权限是否开启。否则用户不能正常使用,也一头雾水,还以为程序出错了。
下面总结一下:相册与麦克风的权限判断(目前只用到了这两个……^_^)
相机:
AVAuthorizationStatus authstatus = [AVCaptureDevice
authorizationStatusForMediaType:AVMediaTypeVideo];
if (authstatus ==
AVAuthorizationStatusRestricted || authstatus ==
AVAuthorizationStatusDenied) //用户关闭了权限
{
UIAlertView *alertView = [[UIAlertView
alloc]
initWithTitle:@""
message:@"相机权限未开启") delegate:self
cancelButtonTitle:NSLocalizedString(@"OK",@"确定")
otherButtonTitles:nil,
nil];
alertView.delegate =
self;
[alertView
show];
}
else
if (authstatus ==
AVAuthorizationStatusNotDetermined) //第一次使用,则会弹出是否打开权限
{
[AVCaptureDevice
requestAccessForMediaType:AVMediaTypeVideo
completionHandler:^(BOOL granted) {
if (granted)
{
_output.metadataObjectTypes =@[AVMetadataObjectTypeQRCode];
}
}];
}
else
if (authstatus ==
AVAuthorizationStatusAuthorized)
{
_output.metadataObjectTypes =@[AVMetadataObjectTypeQRCode];
}
麦克风:
//检测麦克风功能是否打开
[[AVAudioSession
sharedInstance]requestRecordPermission:^(BOOL granted) {
if (!granted)
{
[ViewUtil
alertViewWithString:NSLocalizedString(@"麦克风功能未开启",
nil)];
}
else
{
[self
record:sender];
}
}];