【问题标题】:iOS Camera permissions doesn't appear under settings in some devicesiOS 相机权限不会出现在某些设备的设置下
【发布时间】:2015-11-26 17:33:24
【问题描述】:

我在尝试使用相机时遇到了一些问题。问题是某些设备在设置下向我显示了相机条目,而另一些则没有。在那些没有出现相机开关的设备上,我无法使用相机,因为它没有权限,而且它也没有出现在设置下以启用它们。

这是它在工作设备上的外观:

这就是它在不工作的设备中的样子。

当我截取这些屏幕截图时,应用程序应该已经请求权限,但它没有。

我还验证了这些设备没有启用限制。

有什么想法吗?

更新 1:添加代码

这是我用来显示相机的代码(它在自定义视图下,而不是本机相机视图控制器)

self.captureSession = [[AVCaptureSession alloc] init];
AVCaptureDevice *videoCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error = nil;
AVCaptureDeviceInput *videoInput = [AVCaptureDeviceInput deviceInputWithDevice:videoCaptureDevice error:&error];
if(videoInput)
{
    [self.captureSession addInput:videoInput];
}
else
{
    NSLog(@"Error: %@", error);
}

AVCaptureMetadataOutput *metadataOutput = [[AVCaptureMetadataOutput alloc] init];
[self.captureSession addOutput:metadataOutput];
[metadataOutput setMetadataObjectsDelegate:self
                                     queue:dispatch_get_main_queue()];
[metadataOutput setMetadataObjectTypes:@[AVMetadataObjectTypeCode39Code, AVMetadataObjectTypeQRCode]];

AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession];
previewLayer.frame = self.view.layer.bounds;
UIView * previewView = [[UIView alloc] initWithFrame:self.view.frame];
[previewView.layer addSublayer:previewLayer];
[self.view addSubview:previewView];
[self.view sendSubviewToBack:previewView];

[self.captureSession startRunning];

【问题讨论】:

  • 只有在您的应用请求许可后,相机才会出现在那里。你能显示你用来请求权限的代码吗?
  • 已添加。当我开始捕获会话时,该代码不会自动询问权限吗?应用程序中的其他地方我正在使用 UIImagePickerController 拍照。也许我没有正确处理权限,但是如果我之前打开了 UIImagePickerController,那么权限就被启用了?
  • 我遇到了同样的问题,非常渴望找到解决方案。如果您找到解决此问题的方法,请告诉我...!
  • 亚伦的回答对我有用。
  • 请试试这个解决方案stackoverflow.com/a/34526211它对我有用。

标签: ios iphone ipad ios-camera


【解决方案1】:

您需要在打开会话之前请求许可。使用

[AVCaptureDevice requestAccessForMediaType:completionHandler:]

【讨论】:

  • 不,这不是真的。 “请注意,如果在创建 AVCaptureDeviceInput 时状态为 AVAuthorizationStatusNotDetermined,则将自动显示授权对话框。”如果您不手动请求许可并且在手动完成时确实会询问您,那么它确实可以做到这一点。但是我对 lucaslt89 有同样的问题。即使在重置位置和隐私后,应用程序也不会请求权限,并且在手动调用 [AVCaptureDevice requestAccessForMediaType:completionHandler:] 时结果相同。某些设备会发生这种情况,但其他一些设备则不会发生此类问题。为什么?!!
  • 我也遇到了同样的问题。应用程序没有请求权限。你纠正了吗?
  • 你们能帮忙确认这个问题发生在装有 iOS 13 的多摄像头 iPhone 上吗???
【解决方案2】:

第 1 步:使用以下键(不带引号)在 info.plist 上提供适当的相机消息访问消息

"Privacy - Camera Usage Description"

第 2 步:对于 Objective-C/SWIFT,您需要申请相机访问权限

目标-C

[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted)
        {
            if (granted == true)
            {
                //[self presentViewController           : picker animated:YES completion:NULL];
               //Do your stuff

            }
            else
            {
                UIAlertView *cameraAlert = [[UIAlertView alloc]initWithTitle:STR_APPLICATION_TITLE
                                                                     message:STR_ALERT_CAMERA_DENIED_MESSAGE
                                                                    delegate:self
                                                           cancelButtonTitle:@"OK"
                                                           otherButtonTitles:nil,nil];
                [cameraAlert show];

                NSLog(@"denied");
            }

        }];

斯威夫特

AVCaptureDevice.requestAccessForMediaType(AVMediaTypeVideo, completionHandler: { (videoGranted: Bool) -> Void in
        if (videoGranted) {
             //Do Your stuff here

        } else {
            // Rejected Camera
        }
    })

【讨论】:

    【解决方案3】:

    在将相机类型更改为广角之前,我遇到了同样的问题:

       let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaType.video, position: .back)
    

    【讨论】:

      【解决方案4】:

      我需要相机给用户ARKit。每次显示带有sceneView的vc时,我都会在viewWillAppear中运行checkCameraPermission()。如果用户不允许访问,则会显示带有设置按钮的警报。一旦他们按下该按钮,他们将被带到Settings,并且带有切换开关的相机图标将始终存在。

      override func viewWillAppear(_ animated: Bool) {
          super.viewWillAppear(animated)
      
          checkCameraPermission()
      }
      
      func checkCameraPermission() {
      
          let authorizationStatus = AVCaptureDevice.authorizationStatus(for: AVMediaType.video)
          switch authorizationStatus {
      
          case .notDetermined:
              AVCaptureDevice.requestAccess(for: AVMediaType.video) { [weak self](granted) in
                  if granted {
                      print("access granted")
                      DispatchQueue.main.async { [weak self] in
                          self?.startSceneViewSession()
                      }
      
                  } else {
                      print("access denied")
                      DispatchQueue.main.async { [weak self] in
                          self?.alertUserCameraPermissionMustBeEnabled()
                      }
                  }
              }
      
          case .authorized:
              print("Access authorized")
              startSceneViewSession()
      
          case .denied, .restricted:
              print("restricted")
              alertUserCameraPermissionMustBeEnabled()
      
          @unknown default:
              fatalError()
          }
      }
      
      func alertUserCameraPermissionMustBeEnabled() {
      
          let message = "Camera access is necessary to use Augemented Reality for this app.\n\nPlease go to Settings to allow access to the Camera.\n Please switch the button to the green color."
      
          let alert = UIAlertController (title: "Camera Access Required", message: message, preferredStyle: .alert)
      
          let settingsAction = UIAlertAction(title: "Settings", style: .default, handler: { (action) in
      
              guard let settingsUrl = URL(string: UIApplication.openSettingsURLString) else { return }
      
              if UIApplication.shared.canOpenURL(settingsUrl) {
                  UIApplication.shared.open(settingsUrl, completionHandler: { (_) in
                  })
              }
          })
      
          alert.addAction(settingsAction)
      
          present(alert, animated: true, completion: nil)
      }
      
      // this is for ARKit
      func startSceneViewSession() {
          sceneView.session.run(configuration)
          sceneView.isPlaying = true
      }
      

      【讨论】:

        【解决方案5】:

        如果上述解决方案不起作用,您可以考虑在 iOS 设备上检查此设置。

        【讨论】:

          猜你喜欢
          • 2023-04-03
          • 1970-01-01
          • 1970-01-01
          • 2020-03-31
          • 2022-11-16
          • 2014-10-23
          • 1970-01-01
          • 1970-01-01
          • 2016-12-17
          相关资源
          最近更新 更多