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

早过忘川 提交于 2019-12-06 05:59:10

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/52622390/popup-is-not-coming-to-ask-permission-to-access-camera-in-ios-12

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!