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

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

问题描述

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

在访问相机时,iOS 必须征得客户的许可以允许访问.众所周知,如果客户说不"但后来改变了主意,则无法从您的应用程序中撤消此决定.他们必须转到设置"并执行一些步骤才能重新启用访问权限,即:

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

解决方案

Permission Priming 是一种有效的方式,可以避免您的客户拒绝访问您的应用程序的关键功能.>

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

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

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

以下是一些用于实现此功能的 Swift 3 代码:

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

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

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

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天全站免登陆