在iOS中请求相机权限对话框启动(Prime权限)

在确保最佳体验的同时,提示用户访问摄像机(或其他function)的最有效方法是什么?

访问相机时,iOS必须要求客户允许访问。 众所周知,如果客户说“不”,但后来改变了主意,就没有办法在您的应用程序中改变这个决定。 他们必须转到“设置”并按照多个步骤重新启用访问权限,即:

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

许可启动是避免您的客户可能拒绝访问您的应用程序的关键function的一种有效方法。

在iOS上,应用程序只允许为每个function触发一次默认的系统权限。 权限启动是指应用程序通过模仿系统权限的警报“启动”客户。

这样做的好处是,如果客户select退出(select取消),则应用程序将来仍然可以再次询问,直到他们回答“是” – 此时显示实际的系统权限,而客户的统计数据较less然后有可能改变主意,进入负面的工作stream程。

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

下面是一些Swift 3代码来实现这个function:

[更新:包括是一个解决scheme,打开深层链接到设置,用户可以启用摄像头访问,如果他们以前拒绝它。]

[更新2:添加了分析实施的样本行。]

 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?.cameraTabSelected() // 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) }