【问题标题】:Is there a way to ask user for Camera access after they have already denied it on iOS?有没有办法在用户已经在 iOS 上拒绝相机访问权限后向他们询问?
【发布时间】:2014-11-22 02:30:12
【问题描述】:

我正在使用此代码,但不幸的是它不起作用。

在用户拒绝相机访问后,我想在他们下次尝试加载相机时请求他们允许再次使用相机(在这种情况下,它是使用相机视图的条形码扫描仪)。我总是得到AVAuthorizationStatusDenied,然后granted 总是自动返回NO,即使我在代码中再次要求它。

我的许多用户给我发电子邮件说“我尝试扫描条形码时屏幕是黑的”,这是因为他们出于某种原因拒绝了相机访问。我希望能够再次提示他们,因为拒绝很可能是一个错误。

有没有办法做到这一点?

    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if(authStatus == AVAuthorizationStatusAuthorized)
    {
        NSLog(@"%@", @"You have camera access");
    }
    else if(authStatus == AVAuthorizationStatusDenied)
    {
        NSLog(@"%@", @"Denied camera access");

        [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
            if(granted){
                NSLog(@"Granted access to %@", AVMediaTypeVideo);
            } else {
                NSLog(@"Not granted access to %@", AVMediaTypeVideo);
            }
        }];
    }
    else if(authStatus == AVAuthorizationStatusRestricted)
    {
        NSLog(@"%@", @"Restricted, normally won't happen");
    }
    else if(authStatus == AVAuthorizationStatusNotDetermined)
    {
        NSLog(@"%@", @"Camera access not determined. Ask for permission.");

        [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
            if(granted){
                NSLog(@"Granted access to %@", AVMediaTypeVideo);
            } else {
                NSLog(@"Not granted access to %@", AVMediaTypeVideo);
            }
        }];
    }
    else
    {
        NSLog(@"%@", @"Camera access unknown error.");
    }

【问题讨论】:

  • 可惜没办法再问了。您只需弹出一个 UIAlertView 并让他们知道在设置中启用它。帮助减少人们意外点击“否”的一种选择可能是在您询问并警告用户之前创建一个启动画面,如果他们不点击允许,他们将无法正确使用该应用程序。跨度>

标签: ios objective-c iphone camera


【解决方案1】:

经过一些研究,您似乎无法做我想做的事。如果在 iOS 8+ 上,这是我编写的用于弹出对话框并自动打开设置应用程序的替代方法。

一些注意事项:

  • 从 iOS 10 开始,您需要在 Info.plist 中指定 NSCameraUsageDescription 键才能请求相机访问权限,否则您的应用将在运行时崩溃。
  • 一旦用户更改了您应用的任何权限,它就会终止您的应用。在用户点击“开始”按钮之前,相应地处理并保存任何需要的数据。
  • 在 iOS 8 和 11 之间的某个时间点,Apple 不再要求用户触摸设置应用程序中的隐私单元来访问和更改相机设置。您可能希望根据用户使用的 iOS 版本更改有关用户应该在“设置”应用中执行的操作的说明。如果有人想在下方发表评论,告诉我们具体的 iOS 版本发生了哪些变化,那就太好了。
  • 截至本答案的最后一次编辑,以下代码适用于 iOS 14.2。

Swift 5.2:

在视图控制器的顶部:

import AVFoundation

在打开相机视图之前:

@IBAction func goToCamera()
{
    let status = AVCaptureDevice.authorizationStatus(for: .video)
    switch (status)
    {
    case .authorized:
        self.popCamera()

    case .notDetermined:
        AVCaptureDevice.requestAccess(for: .video) { (granted) in
            if (granted)
            {
                self.popCamera()
            }
            else
            {
                self.camDenied()
            }
        }

    case .denied:
        self.camDenied()

    case .restricted:
        let alert = UIAlertController(title: "Restricted",
                                      message: "You've been restricted from using the camera on this device. Without camera access this feature won't work. Please contact the device owner so they can give you access.",
                                      preferredStyle: .alert)

        let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
        alert.addAction(okAction)
        self.present(alert, animated: true, completion: nil)
    @unknown default:
        fatalError()
    }
}

带有完成块的拒绝警报:

func camDenied()
{
    DispatchQueue.main.async
    {
        var alertText = "It looks like your privacy settings are preventing us from accessing your camera to do barcode scanning. You can fix this by doing the following:\n\n1. Close this app.\n\n2. Open the Settings app.\n\n3. Scroll to the bottom and select this app in the list.\n\n4. Turn the Camera on.\n\n5. Open this app and try again."

        var alertButton = "OK"
        var goAction = UIAlertAction(title: alertButton, style: .default, handler: nil)

        if UIApplication.shared.canOpenURL(URL(string: UIApplication.openSettingsURLString)!)
        {
            alertText = "It looks like your privacy settings are preventing us from accessing your camera to do barcode scanning. You can fix this by doing the following:\n\n1. Touch the Go button below to open the Settings app.\n\n2. Turn the Camera on.\n\n3. Open this app and try again."

            alertButton = "Go"

            goAction = UIAlertAction(title: alertButton, style: .default, handler: {(alert: UIAlertAction!) -> Void in
                UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!, options: [:], completionHandler: nil)
            })
        }

        let alert = UIAlertController(title: "Error", message: alertText, preferredStyle: .alert)
        alert.addAction(goAction)
        self.present(alert, animated: true, completion: nil)
    }
}

目标-C:

在视图控制器的顶部:

#import <AVFoundation/AVFoundation.h>

在打开相机视图之前:

- (IBAction)goToCamera
{
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if(authStatus == AVAuthorizationStatusAuthorized)
    {
        [self popCamera];
    }
    else if(authStatus == AVAuthorizationStatusNotDetermined)
    {
        NSLog(@"%@", @"Camera access not determined. Ask for permission.");
        
        [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted)
        {
            if(granted)
            {
                NSLog(@"Granted access to %@", AVMediaTypeVideo);
                [self popCamera];
            }
            else
            {
                NSLog(@"Not granted access to %@", AVMediaTypeVideo);
                [self camDenied];
            }
        }];
    }
    else if (authStatus == AVAuthorizationStatusRestricted)
    {
        // My own Helper class is used here to pop a dialog in one simple line.
        [Helper popAlertMessageWithTitle:@"Error" alertText:@"You've been restricted from using the camera on this device. Without camera access this feature won't work. Please contact the device owner so they can give you access."];
    }
    else
    {
        [self camDenied];
    }
}

拒绝警报:

- (void)camDenied
{
    NSLog(@"%@", @"Denied camera access");
    
    NSString *alertText;
    NSString *alertButton;
    
    BOOL canOpenSettings = (&UIApplicationOpenSettingsURLString != NULL);
    if (canOpenSettings)
    {
        alertText = @"It looks like your privacy settings are preventing us from accessing your camera to do barcode scanning. You can fix this by doing the following:\n\n1. Touch the Go button below to open the Settings app.\n\n2. Turn the Camera on.\n\n3. Open this app and try again.";
        
        alertButton = @"Go";
    }
    else
    {
        alertText = @"It looks like your privacy settings are preventing us from accessing your camera to do barcode scanning. You can fix this by doing the following:\n\n1. Close this app.\n\n2. Open the Settings app.\n\n3. Scroll to the bottom and select this app in the list.\n\n4. Turn the Camera on.\n\n5. Open this app and try again.";
        
        alertButton = @"OK";
    }
    
    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@"Error"
                          message:alertText
                          delegate:self
                          cancelButtonTitle:alertButton
                          otherButtonTitles:nil];
    alert.tag = 3491832;
    [alert show];
}

对 UIAlertView 的委托调用:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 3491832)
    {
        BOOL canOpenSettings = (&UIApplicationOpenSettingsURLString != NULL);
        if (canOpenSettings)
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
    }
}

【讨论】:

  • 您忘记处理“受限”状态。这与“拒绝”的处理方式完全不同。
  • 您的启动设置代码不正确。这会在“设置”应用中启动您自己的应用设置页面,而不是常规设置。
  • @maddy - 限制有何不同?是的,我希望它转到我自己的应用程序的设置页面...您可以在此处更改隐私设置以允许相机访问。
  • 您应用的设置包不包含相机的隐私设置。那是关于隐私 |设置应用的相机页面。 Restricted 与 Denied 不同。拒绝意味着用户选择拒绝您的应用程序,他们可以运行设置并转到隐私,然后转到相机并打开应用程序。受限意味着某人(如父母)运行“设置”并进入“常规”,然后进入“限制”。然后启用限制(使用密码)并禁用所有应用程序的相机。您的应用用户永远不会有机会允许或拒绝相机访问。
  • @EthanAllen 我在设置页面的启动时得到了纠正。这是 iOS 8 中的一项新功能,所有应用程序现在都出现在“设置”应用程序中,而不仅仅是带有设置包的应用程序。没有设置包的应用会显示隐私设置。
【解决方案2】:

相机访问和照片库访问的完整代码

import AVFoundation

要处理相机动作,请使用以下代码: 方法调用

func openCameraOrLibrary(){
    let imagePicker = UIImagePickerController()
    let alertController : UIAlertController = UIAlertController(title: "Select Camera or Photo Library".localized, message: "", preferredStyle: .actionSheet)
    let cameraAction : UIAlertAction = UIAlertAction(title: "Camera".localized, style: .default, handler: {(cameraAction) in

        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) == true {
            if self.isCamAccessDenied() == false { **//Calling cam access method here**
                imagePicker.sourceType = .camera
                imagePicker.delegate = self
                self.present(imagePicker, animated: true, completion: nil)
            }

        }else{
            self.present(self.showAlert(Title: "", Message: "Camera is not available on this Device or accesibility has been revoked!".localized), animated: true, completion: nil)
            self.showTabbar()

        }

    })

    let libraryAction : UIAlertAction = UIAlertAction(title: "Photo Library", style: .default, handler: {(libraryAction) in

        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary) == true {

            imagePicker.sourceType = .photoLibrary
            imagePicker.delegate = self
            self.present(imagePicker, animated: true, completion: nil)

        }else{
            self.showTabbar()
            self.present(self.showAlert(Title: "", Message: "Photo Library is not available on this Device or accesibility has been revoked!".localized), animated: true, completion: nil)
        }
    })

    let cancelAction : UIAlertAction = UIAlertAction(title: "Cancel".localized, style: .cancel , handler: {(cancelActn) in
        self.showTabbar()
    })

    alertController.addAction(cameraAction)

    alertController.addAction(libraryAction)

    alertController.addAction(cancelAction)

    alertController.popoverPresentationController?.sourceView = view
    alertController.popoverPresentationController?.sourceRect = view.frame

    self.present(alertController, animated: true, completion: nil)
    self.hideTabbar()

}

处理相机访问功能的方法

func isCamAccessDenied()-> Bool
{
    let status = AVCaptureDevice.authorizationStatus(for: AVMediaType.video)
    if status == .restricted || status == .denied {
    DispatchQueue.main.async
        {
            var alertText = "It looks like your privacy settings are preventing us from accessing your camera to do barcode scanning. You can fix this by doing the following:\n\n1. Close this app.\n\n2. Open the Settings app.\n\n3. Scroll to the bottom and select this app in the list.\n\n4. Turn the Camera on.\n\n5. Open this app and try again."

            var alertButton = "OK"
            var goAction = UIAlertAction(title: alertButton, style: .default, handler: nil)

            if UIApplication.shared.canOpenURL(URL(string: UIApplicationOpenSettingsURLString)!)
            {
                alertText = "It looks like your privacy settings are preventing us from accessing your camera to do barcode scanning. You can fix this by doing the following:\n\n1. Touch the Go button below to open the Settings app.\n\n2. Turn the Camera on.\n\n3. Open this app and try again."

                alertButton = "OK"

                goAction = UIAlertAction(title: alertButton, style: .default, handler: {(alert: UIAlertAction!) -> Void in
                    UIApplication.shared.open(URL(string: UIApplicationOpenSettingsURLString)!, options: [:], completionHandler: nil)
                })
            }

            let alert = UIAlertController(title: "Error", message: alertText, preferredStyle: .alert)
            alert.addAction(goAction)
            self.present(alert, animated: true, completion: nil)
        }
        return true
    }
    return false
}

【讨论】:

    【解决方案3】:

    对于 Swift 3.0

    这将引导用户进入更改权限的设置。

    func checkCameraAuthorise() -> Bool {
        let status = AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo)
        if status == .restricted || status == .denied {
            let dialog = ZAlertView(title: "", message: "Please allow access to the camera in the device's Settings -> Privacy -> Camera", isOkButtonLeft: false, okButtonText: "OK", cancelButtonText: "Cancel", okButtonHandler:
                { _ -> Void in UIApplication.shared.openURL(URL(string: UIApplicationOpenSettingsURLString)!)}, cancelButtonHandler: { alertView in alertView.dismissAlertView() })
            dialog.show()
            return false
        }
        return true
    }
    

    【讨论】:

      【解决方案4】:

      一旦他们拒绝了相机访问权限,用户就可以在“设置”中授权您的应用使用相机。按照设计,您不能在自己的代码中覆盖它。

      您可以使用以下示例代码检测这种情况,然后向用户解释如何修复它:iOS 7 UIImagePickerController Camera No Image

      NSString *mediaType = AVMediaTypeVideo; // Or AVMediaTypeAudio
      
      AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
      
      // The user has explicitly denied permission for media capture.
      else if(authStatus == AVAuthorizationStatusDenied){
          NSLog(@"Denied");
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-10-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多