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

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)

}

在Xamarin.iOS,启用相机权限通常涉及以下几个步骤: 1. **导入必要的库**:首先,在你的C#项目添加`UIKit`和`AVFoundation`框架引用,这两个框架分别负责用户界面和多媒体处理。 ```csharp using UIKit; using AVFoundation; ``` 2. **获取授权**:在需要使用相机的地方,显示一个授权请求。这通常是通过`UIImagePickerController`来做的,它允许你向用户请求特定的媒体访问权限。在C#,你可以这样做: ```csharp UIImagePickerController request = new UIImagePickerController(); request.sourceType = UIImagePickerControllerSourceType.Camera; request.allowsEditing = false; // 是否允许编辑图片 if (UIImagePickerController.IsCameraAvailable) { // 判断相机是否可用 if (UIApplication.SharedApplication.InvokeOnMainThread(() => { // 在主线程显示授权请求 UIApplication.SharedApplication.PresentViewController(request, true, null); })) { // 如果成功,进入相机界面 } } else { Console.WriteLine("Camera is not available"); } ``` 3. **处理结果**:当用户选择完成操作后,你需要从回调处理结果,看看他们是否给予了你访问相机权限: ```csharp public void ImagePickerControllerCancelled(UIPickerViewController picker) { // 用户取消了请求 } public void ImagePickerController didFinishPickingMediaWithInfo(UIImagePickerController picker, NSDictionary info) { if (info[UIImagePickerController.InfoKey] != null) { UIImage selectedImage = (UIImage)info[UIImagePickerController.InfoKey]; // 现在你可以使用selectedImage处理捕获的照片 } picker.DismissViewController(true, null); } ``` 记得在`Info.plist`文件设置`NSCameraUsageDescription`键,提供一个简短的提示告诉用户为什么需要访问相机
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值