如何检测用户已单击“不允许访问摄像头” [英] How to detect user has clicked Don't Allow access to camera
问题描述
我正在使用 UIImagePicker
向用户展示相机以拍摄将在应用中使用的照片。
I am using a UIImagePicker
to present the users with camera to take photos which will be used in the app.
我的问题是,当用户第一次打开图像选择器时,会向他们显示一条提示:'我的应用程序想要使用两个选项访问您的相机,不允许和确定。
My problem is that on the first time a user opens the image picker they are presented with a prompt saying: '"my App" Would like to Access your Camera' with two options, Don't allow and OK.
我的要求是,当用户点击不允许时,图像选择器将被解除,留下黑色视图。有没有办法检测用户选择了不允许?
My requirement is that when the user clicks Don't Allow, the Image picker gets dismissed leaving a black view. Is there a way to detect that the user has chosen Don't allow?
这是我的代码来呈现 UIImagePicker
:
var PhotoPicker:UIImagePickerController = UIImagePickerController()
PhotoPicker.delegate = self
PhotoPicker.sourceType = .Camera
PhotoPicker.cameraFlashMode = .Off
PhotoPicker.showsCameraControls = false
PhotoPicker.cameraDevice = .Rear
self.presentViewController(PhotoPicker, animated: false, completion: nil)
推荐答案
要检测对您图书馆的访问权限:
您需要使用AssetsLibrary。首先,导入资产库框架:
You need to use AssetsLibrary for that. First, import assets library framework:
import AssetsLibrary
然后,请求授权状态,如果未确定,请使用块来捕获这些事件,如下所示:
Then, request authorization status, and if it is not determined, use blocks to catch those events, like this:
if ALAssetsLibrary.authorizationStatus() == ALAuthorizationStatus.NotDetermined {
let library = ALAssetsLibrary()
library.enumerateGroupsWithTypes(.All, usingBlock: { (group, stop) -> Void in
// User clicked ok
}, failureBlock: { (error) -> Void in
// User clicked don't allow
imagePickerController.dismissViewControllerAnimated(true, completion: nil)
})
}
要检测对相机的访问权限:
您需要使用AVFoundation。首先,导入avfoundation框架:
You need to use AVFoundation for that. First, import avfoundation framework:
import AVFoundation
然后,如前所述,当您转到imagepicker并捕获事件时请求用户权限。
Then, as previously, request user permission when you go to imagepicker and catch the event.
if AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo) == AVAuthorizationStatus.NotDetermined {
AVCaptureDevice.requestAccessForMediaType(AVMediaTypeVideo, completionHandler: { (videoGranted: Bool) -> Void in
// User clicked ok
if (videoGranted) {
// User clicked don't allow
} else {
imagePickerController.dismissViewControllerAnimated(true, completion: nil)
}
})
}
希望有所帮助!
这篇关于如何检测用户已单击“不允许访问摄像头”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!