ios 请在设置中打开相机权限_在iOS中请求相机权限对话启动(Prime权限)

本文探讨了在iOS系统中有效提示用户提供相机访问权限的方法。iOS访问相机需用户授权,若用户拒绝只能去设置中重新开启。介绍了Permission Priming方法避免用户拒绝关键功能权限,还给出了Swift 3代码实现该功能,包括权限检查、提示等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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: "\"\" Would Like To Access the Camera", message: "Please grant permission to use the Camera so that you can .", 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: "\"\" Would Like To Access the Camera", message: " would like to access your Camera so that you can .", 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)

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值