Open camera function with authorisation status iOS swift

 Problem:

I want to call one function that can be check for camera access permissions for my app is allowed.Then it have to call the image picker to pick the image from camera.How can i achieve this?

Solution:


    lazy var picker:UIImagePickerController! = UIImagePickerController()



    //    MARK: - Open camera

    func openCamera(){

        let authStatus = AVCaptureDevice.authorizationStatus(for: AVMediaType.video)

        switch (authStatus){

        case .notDetermined:

            AVCaptureDevice.requestAccess(for: AVMediaType.video, completionHandler: { (granted: Bool) -> Void in

                if granted == true {

                    DispatchQueue.main.async() {

                        if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerController.SourceType.camera)) {

                            self.picker = UIImagePickerController()

                            self.picker.delegate =  self

                            self.picker.sourceType = UIImagePickerController.SourceType.camera

                            self.present(self.picker, animated: true, completion: nil)

                        }

                    }

                }})


        case .restricted,.denied:

            self.showAlert(msg:"Camera access denied", controller: self)


        case .authorized:

            if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerController.SourceType.camera)) {

                picker = UIImagePickerController()

                picker.delegate =  self

                picker.sourceType = UIImagePickerController.SourceType.camera

                self.present(picker, animated: true, completion: nil)

            }


        @unknown default:

            break

        }

    }

Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

Convert NsNumber, NSDate to String in iOS Swift

Global LocationManager Singleton class iOS Swift