Is there a way to ask user for Camera access after they have already denied it on iOS?

后端 未结 4 1090
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-07 12:35

I am using this code, but unfortunately it doesn\'t work.

After a user has denied camera access, I want to ask them for permission to use the camera again the next t

4条回答
  •  遥遥无期
    2020-12-07 12:37

    Full code of Camera access and photo library access

    import AVFoundation
    

    To handle camera action use the below code: Method calling

    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()
    
    }
    

    Method to handle camera access functionality

    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
    }
    

提交回复
热议问题