iOS中的请求相机权限对话框启动(Prime Permissions) [英] Request Camera Permission Dialog Priming (Prime Permissions) in iOS

查看:85
本文介绍了iOS中的请求相机权限对话框启动(Prime Permissions)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在确保最佳体验的同时,提示用户提供对相机(或其他功能)的访问权限的最有效方法是什么?

访问相机时,iOS 必须请求客户许可才能访问.众所周知,如果客户说不"但随后改变主意,则无法从您的应用程序中撤销此决定.他们必须转到设置"并按照多个步骤重新启用访问权限,即:

设置->隐私->相机 ->[您的应用程序] ->打开开关

解决方案

Permission Priming 是避免客户拒绝访问您应用的关键功能的一种有效方法.p>

在 iOS 上,每个功能仅允许应用触发一次默认系统权限.权限启动是指应用向客户启动"模拟系统权限的警报.

这样做的好处是,如果客户选择退出(选择取消),应用程序仍然可以在未来再次询问,直到他们说是 - 此时会显示实际的系统权限并且客户从统计学上讲,他们改变主意并进入负面工作流程的可能性较小.

此外,由于 cameraSelected() 执行此工作流程,如果用户拒绝,但在未来某个时间确实更改了他们的设置,应用程序将立即反映新的权限无需进一步输入(即用户可以切换到设置,更改权限,然后切换回应用程序).

这里有一些 Swift 3 代码来实现这个功能:

[更新:包括一个解决方案,用于打开指向设置的深层链接,用户可以在其中启用相机访问,如果他们以前拒绝过.]

[更新 2:为 Analytics 实施添加了示例行.]

func cameraSelected() {//首先我们检查设备是否有摄像头(否则会在模拟器中崩溃 - 此外,某些 iPod touch 型号没有摄像头).如果让 deviceHasCamera = UIImagePickerController.isSourceTypeAvailable(.camera) {让 authStatus = AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo)切换 authStatus {案例.授权:showCameraPicker()案例.拒绝:alertPromptToAllowCameraAccessViaSettings()案例.未确定:权限PrimeCameraAccess()默认:权限PrimeCameraAccess()}} 别的 {let alertController = UIAlertController(title: "Error", message: "Device has no camera", preferredStyle: .alert)让 defaultAction = UIAlertAction(title: "OK", style: .default, handler: { (alert) inAnalytics.track(事件:.permissionsPrimeCameraNoCamera)})alertController.addAction(defaultAction)存在(alertController,动画:真,完成:无)}}func alertPromptToAllowCameraAccessViaSettings() {let alert = UIAlertController(title: ""<Your App>" Want To Access the Camera", message: "请授予使用相机的权限,以便您可以<客户受益>.", preferredStyle: .警报 )alert.addAction(UIAlertAction(title: "打开设置", style: .cancel) { alert inAnalytics.track(事件:.permissionsPrimeCameraOpenSettings)如果让 appSettingsURL = NSURL(string: UIApplicationOpenSettingsURLString) {UIApplication.shared.openURL(appSettingsURL)}})存在(警报,动画:真,完成:无)}功能权限PrimeCameraAccess(){let alert = UIAlertController(title: ""<Your App>" 想要访问相机", message: "<Your App> 想要访问你的相机以便你能<客户受益>.", 首选样式: .alert )let allowAction = UIAlertAction(title: "Allow", style: .default, handler: { (alert) -> Void inAnalytics.track(事件:.permissionsPrimeCameraAccepted)if AVCaptureDevice.devices(withMediaType: AVMediaTypeVideo).count >0 {AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo, completionHandler: { [weak self] 授予DispatchQueue.main.async {self?.cameraSelected()//再试一次}})}})alert.addAction(allowAction)让 dedeclineAction = UIAlertAction(title: "Not Now", style: .cancel) { (alert) inAnalytics.track(事件:.permissionsPrimeCameraCancelled)}alert.addAction(declineAction)存在(警报,动画:真,完成:无)}func showCameraPicker() {让选择器 = UIImagePickerController()picker.delegate = selfpicker.modalPresentationStyle = UIModalPresentationStyle.currentContextpicker.allowsEditing = falsepicker.sourceType = UIImagePickerControllerSourceType.camera现在(选择器,动画:真,完成:无)}

What is the most effective way to prompt a User to provide access to the Camera (or other feature), while ensuring the best experience?

When accessing the Camera, iOS must ask the Customer permission to allow access. As we all know, if the Customer says "No" but then changes their mind, there is no way to reverse this decision from within your App. They must go to Settings and follow a number of steps to re-enable access, namely:

Settings -> Privacy -> Camera -> [Your App] -> turn switch on

解决方案

Permission Priming is an effective way to avoid a situation where your Customer might deny access to a key feature of your app.

On iOS an App is only allowed to trigger the default system permission once per feature. Permission priming is when an app "primes" the Customer with an alert that mimics a system permission.

The benefit to doing this is so that if the Customer opts-out (selects Cancel), the App is still able to ask again in future, until they say yes — at which time the actual system permission is displayed and the Customer is statistically less likely to then change their mind and enter into the negative work flow.

Furthermore, since cameraSelected() performs this workflow, if the user declines, but then at some future point does change their settings, the App will immediately reflect the new permissions without further input (ie. the User could switch to Settings, change permissions, and then switch back to the App).

Here is some Swift 3 code to implement this feature:

[UPDATE: Included is a solution to open a deep-link to Settings where the User can enable camera access, if they have previously denied it.]

[UPDATE 2: Added sample lines for Analytics implementation.]

func cameraSelected() {
    // First we check if the device has a camera (otherwise will crash in Simulator - also, some iPod touch models do not have a camera).
    if let deviceHasCamera = UIImagePickerController.isSourceTypeAvailable(.camera) {
        let authStatus = AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo)
        switch authStatus {
            case .authorized:
                showCameraPicker()
            case .denied:
                alertPromptToAllowCameraAccessViaSettings()
            case .notDetermined:
                permissionPrimeCameraAccess()
            default:
                permissionPrimeCameraAccess()
        }
    } else {
        let alertController = UIAlertController(title: "Error", message: "Device has no camera", preferredStyle: .alert)
        let defaultAction = UIAlertAction(title: "OK", style: .default, handler: { (alert) in
            Analytics.track(event: .permissionsPrimeCameraNoCamera)
        })
        alertController.addAction(defaultAction)
        present(alertController, animated: true, completion: nil)
    }
}


func alertPromptToAllowCameraAccessViaSettings() {
    let alert = UIAlertController(title: ""<Your App>" Would Like To Access the Camera", message: "Please grant permission to use the Camera so that you can  <customer benefit>.", preferredStyle: .alert )
    alert.addAction(UIAlertAction(title: "Open Settings", style: .cancel) { alert in
        Analytics.track(event: .permissionsPrimeCameraOpenSettings)
        if let appSettingsURL = NSURL(string: UIApplicationOpenSettingsURLString) {
          UIApplication.shared.openURL(appSettingsURL)
        }
    })
    present(alert, animated: true, completion: nil)
}


func permissionPrimeCameraAccess() {
    let alert = UIAlertController( title: ""<Your App>" Would Like To Access the Camera", message: "<Your App> would like to access your Camera so that you can <customer benefit>.", preferredStyle: .alert )
    let allowAction = UIAlertAction(title: "Allow", style: .default, handler: { (alert) -> Void in
        Analytics.track(event: .permissionsPrimeCameraAccepted)
        if AVCaptureDevice.devices(withMediaType: AVMediaTypeVideo).count > 0 {
            AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo, completionHandler: { [weak self] granted in
                DispatchQueue.main.async {
                    self?.cameraSelected() // try again
                }
            })
        }
    })
    alert.addAction(allowAction)
    let declineAction = UIAlertAction(title: "Not Now", style: .cancel) { (alert) in
        Analytics.track(event: .permissionsPrimeCameraCancelled)
    }
    alert.addAction(declineAction)
    present(alert, animated: true, completion: nil)
}


func showCameraPicker() {
    let picker = UIImagePickerController()
    picker.delegate = self
    picker.modalPresentationStyle = UIModalPresentationStyle.currentContext
    picker.allowsEditing = false
    picker.sourceType = UIImagePickerControllerSourceType.camera
    present(picker, animated: true, completion: nil)
}

这篇关于iOS中的请求相机权限对话框启动(Prime Permissions)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆