首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在iOS 8中显示相机权限对话框

在iOS 8中显示相机权限对话框
EN

Stack Overflow用户
提问于 2014-09-12 07:43:01
回答 9查看 77.1K关注 0票数 74

当我的应用程序第一次尝试在iOS 8上访问相机时,用户会看到一个相机权限对话框,就像iOS 7中用于麦克风访问的麦克风对话框一样。

在iOS 7中,可以提前调用麦克风权限对话框并查看权限是否被授予(例如,请参见this question )。是否有类似的方法来调用iOS 8中的相机权限对话框?该对话框可以组合为麦克风和相机访问权限吗?

EN

回答 9

Stack Overflow用户

回答已采纳

发布于 2014-09-27 06:45:19

下面是我们最后使用的方法:

代码语言:javascript
运行
复制
if ([AVCaptureDevice respondsToSelector:@selector(requestAccessForMediaType: completionHandler:)]) {
    [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
        // Will get here on both iOS 7 & 8 even though camera permissions weren't required 
        // until iOS 8. So for iOS 7 permission will always be granted.
        if (granted) {
            // Permission has been granted. Use dispatch_async for any UI updating
            // code because this block may be executed in a thread.
            dispatch_async(dispatch_get_main_queue(), ^{
                [self doStuff];
            });                
        } else {
            // Permission has been denied.
        }
    }];
} else {
    // We are on iOS <= 6. Just do what we need to do.
    [self doStuff];
}
票数 91
EN

Stack Overflow用户

发布于 2014-09-23 18:42:53

我遇到了一个类似的问题,如果用户在第一次提示时拒绝了对相机的访问,按下按钮就可以在照相机模式下的黑色屏幕上拍摄快照结果。

但是,我想检测到用户已经拒绝了访问,并提示他们必须打开,但我找不到任何函数来检查当前用户的相机访问,有这样的功能吗?

编辑:下面的检查将让您知道在IOS 8中有关相机访问:

代码语言:javascript
运行
复制
#import <AVFoundation/AVFoundation.h>

AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    
    if(status == AVAuthorizationStatusAuthorized) { // authorized
        
    }
    else if(status == AVAuthorizationStatusDenied){ // denied
        
    }
    else if(status == AVAuthorizationStatusRestricted){ // restricted
        
        
    }
    else if(status == AVAuthorizationStatusNotDetermined){ // not determined
        
        [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
            if(granted){ // Access has been granted ..do something
               
            } else { // Access denied ..do something
               
            }
        }];
    }

这些信息是关于以下问题(How to know that application have camera access or not programmatically in iOS8)的:

票数 62
EN

Stack Overflow用户

发布于 2015-06-27 08:39:34

这是我的Swift解决方案(iOS 8),我需要相机的QR扫描,所以真的必须迅速使用它。

这提供了

  1. 鼓励用户在默认的“允许相机访问”问题之前选择“允许”
  2. 如果用户拒绝了第一个请求,就可以方便地访问设置。

为了让它运行调用,检查ViewDidAppear /或ViewDidLoad等中的摄像机。我需要使用viewDidAppear,因此设置了自定义的相机视图约束。

代码语言:javascript
运行
复制
func checkCamera() {
    let authStatus = AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo)
    switch authStatus {
    case .authorized: break // Do your stuff here i.e. allowScanning()
    case .denied: alertToEncourageCameraAccessInitially()
    case .notDetermined: alertPromptToAllowCameraAccessViaSetting()
    default: alertToEncourageCameraAccessInitially()
    }
}

func alertToEncourageCameraAccessInitially() {
    let alert = UIAlertController(
        title: "IMPORTANT",
        message: "Camera access required for QR Scanning",
        preferredStyle: UIAlertControllerStyle.alert
    )
    alert.addAction(UIAlertAction(title: "Cancel", style: .default, handler: nil))
    alert.addAction(UIAlertAction(title: "Allow Camera", style: .cancel, handler: { (alert) -> Void in
        UIApplication.shared.openURL(URL(string: UIApplicationOpenSettingsURLString)!)
    }))
    present(alert, animated: true, completion: nil)
}

func alertPromptToAllowCameraAccessViaSetting() {

    let alert = UIAlertController(
        title: "IMPORTANT",
        message: "Please allow camera access for QR Scanning",
        preferredStyle: UIAlertControllerStyle.alert
    )
    alert.addAction(UIAlertAction(title: "Dismiss", style: .cancel) { alert in
        if AVCaptureDevice.devices(withMediaType: AVMediaTypeVideo).count > 0 {
            AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo) { granted in
                DispatchQueue.main.async() {
                    self.checkCamera() } }
        }
        }
    )
    present(alert, animated: true, completion: nil)
}

感谢上面给出的使用dispatch_async的提示-使响应显示新设置的相机功能要快得多。

很抱歉尾随的闭包混在一起。想试一试。

票数 54
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25803217

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档