CodeScanner icon indicating copy to clipboard operation
CodeScanner copied to clipboard

How do I get notified when user denies camera permission?

Open haemi opened this issue 4 years ago • 4 comments

Is there a delegate method that handles this case?

haemi avatar Feb 11 '21 15:02 haemi

Having the same problem - the permission error is currently unhandled: https://github.com/twostraws/CodeScanner/blob/main/Sources/CodeScanner/CodeScanner.swift#L172

ralfebert avatar Apr 01 '21 09:04 ralfebert

I would hope this is reported back as one of the ScanError types, but if it isn't let me know!

twostraws avatar Dec 14 '21 16:12 twostraws

@twostraws there is no error reported when the user denies camera access on the initial permission request. Subsequent initializations of the AVCaptureDeviceInput throw an exception with code AVError.Code.applicationIsNotAuthorizedToUseDevice, which correctly filters back up the delegate chain.

You would need to explicitly manage the permission request rather than relying on the implicit request which is generated for you in the AVCaptureDeviceInput initializer. See AVCaptureDevice.authorizationStatus(for:) and AVCaptureDevice.requestAccess(for:completionHandler:). Then, put your setup code behind these requests to cover the initial denial.

I made a sample Pull Request illustrating how this could be done. #49

rholstad avatar Dec 20 '21 17:12 rholstad

You can use:

func checkCameraPermission() { if AVCaptureDevice.authorizationStatus(for: .video) == .authorized { //already authorized print("already authorized") } else { AVCaptureDevice.requestAccess(for: .video, completionHandler: { (granted: Bool) in if granted { //access allowed print("access allowed") } else { //access denied showingCameraAlert = true } }) } }

and

`.alert(isPresented: $showingCameraAlert) { Alert( title: Text("Camera access"), message: Text("This app uses the camera to scan barcode. Please go to Settings and allow camera access to proceed."), primaryButton: .default(Text("Settings")) { if let settingsUrl = URL(string: UIApplication.openSettingsURLString) {

                    UIApplication.shared.open(settingsUrl)
                    
                }
            },
            secondaryButton: .cancel()
        )
    }`

emog avatar Apr 05 '22 12:04 emog

The completion now returns an error with .permissionDenied in this case.

nathanfallet avatar Aug 28 '22 15:08 nathanfallet