在iOS中检测相机的权限 [英] Detect permission of camera in iOS

查看:184
本文介绍了在iOS中检测相机的权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个非常简单的视频应用程序。我使用官方控件:UIImagePickerController。

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

这里是问题。当第一次呈现UIImagePickerController时,iOS将请求权限。用户可以单击是或否。如果用户单击否,则不会取消控制。相反,如果用户保持点击开始按钮,则定时器继续,而屏幕总是黑色,并且用户不能停止定时器或返回。用户唯一可以做的就是杀死应用程序。下一次呈现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.

我想知道这是否是一个错误。是否有任何方式我们可以检测相机的权限,以便我们可以决定显示UIImagePickerController或者不是?

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?

推荐答案

AVAuthorizationStatus 并正确处理这些情况。

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
}

这篇关于在iOS中检测相机的权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆