gpt4 book ai didi

ios - 在 iOS 中请求相机权限对话框启动(主要权限)

转载 作者:IT王子 更新时间:2023-10-29 05:29:26 25 4
gpt4 key购买 nike

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

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

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

最佳答案

Permission Priming 是一种有效的方法,可以避免您的客户可能拒绝访问您应用的关键功能。

在 iOS 上,应用程序仅允许针对每个功能触发一次默认系统权限。权限启动是指应用程序通过模拟系统权限的警报“启动”客户。

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

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

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

[更新:包括一个解决方案,用于打开指向设置的深层链接,用户可以在其中启用摄像头访问权限(如果他们之前拒绝过)。]

[更新 2:添加了用于 Analytics 实现的示例行。]

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 - 在 iOS 中请求相机权限对话框启动(主要权限),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41303746/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com