Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Popup is not coming to ask permission to access camera in iOS 12

Tags:

ios

ios12

As per the apple standard, we need to ask permission to access user camera. so I have successfully integrated camera and it is working fine in iOS 11. but currently, I am testing camera feature and found that if user one time allowed camera access, The same app will not ask for permission even after fresh installed(from app store).

so my question is, is it behavious changed in iOS 12 or we need to do some setup to asked permission every time when user try to install fresh App?

Thanks

like image 298
Jatin Patel - JP Avatar asked Oct 03 '18 07:10

Jatin Patel - JP


1 Answers

iOS 12.1 / Swift 4.2

Every time the user taps on the Camera button in your app, you call this code. It firstly asks for permissions, and if the settings are still there from the past installs, UIAlertController pops up, allowing the user to open the Settings app on the device, and change camera permission settings.

OnCameraOpenButtonTap()

if UIImagePickerController.isSourceTypeAvailable(.camera) {
   checkCameraAccess(isAllowed: {
            if $0 {
                DispatchQueue.main.async {
                    self.presentCamera()
                }
            } else {
                DispatchQueue.main.async {
                self.presentCameraSettings()
            }
        }
    })
}

func checkCameraAccess(isAllowed: @escaping (Bool) -> Void) {
    switch AVCaptureDevice.authorizationStatus(for: .video) {
    case .denied:
        isAllowed(false)
    case .restricted:
        isAllowed(false)
    case .authorized:
        isAllowed(true)
    case .notDetermined:
        AVCaptureDevice.requestAccess(for: .video) { isAllowed($0) }
    }
}

private func presentCamera() {
    let imagePicker = UIImagePickerController()
    imagePicker.delegate = self
    imagePicker.sourceType = .camera
    present(imagePicker, animated: true, completion: nil)
}

private func presentCameraSettings() {
    let alert = UIAlertController.init(title: "allowCameraTitle", message: "allowCameraMessage", preferredStyle: .alert)
    alert.addAction(UIAlertAction.init(title: "Cancel", style: .cancel, handler: { (_) in
    }))

    alert.addAction(UIAlertAction.init(title: "Settings", style: .default, handler: { (_) in
        UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!, options: [:], completionHandler: nil)
    }))

    present(alert, animated: true)
}
like image 51
Boris Nikolic Avatar answered Oct 16 '22 16:10

Boris Nikolic