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

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

问题描述

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



访问相机时,iOS必须要求客户允许访问权限。众所周知,如果客户说不但随后改变主意,则无法在您的应用程序中撤销此决定。他们必须转到设置并按照一系列步骤重新启用访问权限,即:



设置 - >隐私 - >相机 - > [您的应用] - >开启

解决方案

权限启动是避免的有效方法您的客户可能拒绝访问您应用的关键功能的情况。



在iOS上,每个功能只允许应用程序触发一次默认系统权限。权限启动是指应用程序使用模拟系统权限的警报填充客户。



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

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



以下是一些 Swift 3 代码来实现此功能:



[更新:包含的是解决方案,打开设置的深层链接,用户可以启用相机交流cess,如果他们之前已经否认了它。]



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

  func cameraSelected(){
//首先我们检查设备是否有摄像头(否则会在模拟器中崩溃 - 也是,有些iPod touch机型没有相机)。
如果让deviceHasCamera = UIImagePickerController.isSourceTypeAvailable(.camera){
let authStatus = AVCaptureDevice.authorizationStatus(forMediaType:AVMediaTypeVideo)
switch authStatus {
case .authorized:
showCameraPicker()
case .denied:
alertPromptToAllowCameraAccessViaSettings()
case .notDetermined:
permissionPrimeCameraAccess()
默认值:
permissionPrimeCameraAccess()
}
} else {
let alertController = UIAlertController(标题:错误,消息:设备没有摄像头,preferredStyle:.alert)
让defaultAction = UIAlertAction(标题:OK ,style:.default,handler:{(alert)in
Analytics.track(event:.permissionsPrimeCameraNoCamera)
})
alertController.addAction(defaultAction)
present(alertController) ,一个imated:true,completion:nil)
}
}


func alertPromptToAllowCameraAccessViaSettings(){
let alert = UIAlertController(title:\) <您的应用> \想要访问相机,消息:请授予使用相机的权限,以便您可以< customer benefit>。,preferredStyle:.alert)
alert.addAction (UIAlertAction(标题:打开设置,样式:.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> \想要访问相机,消息:< Your App>想要访问你的相机,以便你可以< 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: {[弱自我]授予
DispatchQueue.main.async {
self?.cameraSelected()//再试一次
}
})
}
})
alert.addAction(allowAction)
让$ A $ $ Analytics.track中的declineAction = UIAlertAction(标题:Not Now,style:.cancel){(alert)(事件:。 permissionsPrimeCameraCancelled)
}
alert.addAction(declineAction)
present(alert,animated:true,completion:nil)
}


福nc showCameraPicker(){
let picker = UIImagePickerController()
picker.delegate = self
picker.modalPresentationStyle = UIModalPresentationStyle.currentContext
picker.allowsEditing = false
picker。 sourceType = UIImagePickerControllerSourceType.camera
present(picker,animated:true,completion: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权限)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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