How to check if app has Camera permission on iOS

TomaszBTomaszB PLUniversity
edited October 2015 in Xamarin.Forms

I need to check if appliaction has camera permission before launching ZXing barcode scanner or Augumented Reality page.

If I just launch them and user has disabled camera access in ios settings - barcode scanner either crashes or never returns and AR just crashes.

I tried :
UIImagePickerController.IsSourceTypeAvailable(UIImagePickerControllerSourceType.Camera);
but it returns true = camera supported.

Exception generated by launching AR (Wikitude)
ex {Foundation.MonoTouchException: Objective-C exception thrown. Name: NSGenericException Reason: *** AVCaptureSession can't startRunning between calls to beginConfiguration / commitConfiguration Native stack trace: 0 CoreFoundation 0x000000018514e0a0 <redacted> + 160 1 libobjc.A.dylib 0x0000000195afc0e4 objc_exception_throw + 60 2

If app has camera permission AR starts OK.

Tagged:

Best Answer

Answers

  • FredyWengerFredyWenger CHInsider ✭✭✭✭✭

    @Tomaszb:
    Why don't you set the camera-permission to your app?

  • TomaszBTomaszB PLUniversity

    Thanks Adam, this is what was needed. I re-worded it to Xamarin API syntax.

            public static bool IsCameraAuthorized(){            
                    AVAuthorizationStatus authStatus = AVCaptureDevice.GetAuthorizationStatus(AVMediaType.Video);
                    if(authStatus == AVAuthorizationStatus.Authorized) {
                        // do your logic
                        return true;
                    } else if(authStatus == AVAuthorizationStatus.Denied){
                        // denied
                        return false;
                    } else if(authStatus == AVAuthorizationStatus.Restricted){
                        // restricted, normally won't happen
                        return false;
                    } else if(authStatus == AVAuthorizationStatus.NotDetermined){
                        // not determined?!
                        return false;
                    } else {
                        return false;
                        // impossible, unknown authorization status
                    }
                }    
    
Sign In or Register to comment.