ios 判断打开相机权限_在iOS中检测相机的权限

I am developing a very simple video app. I use the official control: UIImagePickerController.

Here is the problem. When presenting the UIImagePickerController for the first time, the iOS will ask for the permission. The user can click yes or no. If the user clicks no, the control is not dismissed. Instead, if the user keeps clicking the start button, the timers go on while the screen is always black, and the user can't stop the timers or go back. The only thing the user can do is to kill the app. The next time the UIImagePickerController is presented, it is still a black screen and the user can't go back if clicking start.

I was wondering if it's a bug. Is there any way we can detect the permission of the camera so that we can decide to show the UIImagePickerController or not?

解决方案

Check the AVAuthorizationStatus and handle the cases properly.

NSString *mediaType = AVMediaTypeVideo;

AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];

if(authStatus == AVAuthorizationStatusAuthorized) {

// do your logic

} else if(authStatus == AVAuthorizationStatusDenied){

// denied

} else if(authStatus == AVAuthorizationStatusRestricted){

// restricted, normally won't happen

} else if(authStatus == AVAuthorizationStatusNotDetermined){

// not determined?!

[AVCaptureDevice requestAccessForMediaType:mediaType completionHandler:^(BOOL granted) {

if(granted){

NSLog(@"Granted access to %@", mediaType);

} else {

NSLog(@"Not granted access to %@", mediaType);

}

}];

} else {

// impossible, unknown authorization status

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值