[英]How to detect user has clicked Don't Allow access to camera
I am using a UIImagePicker
to present the users with camera to take photos which will be used in the app. 我正在使用
UIImagePicker
向用户展示相机以拍摄将在应用中使用的照片。
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?
有没有办法检测用户选择了不允许?
Here is my code to present UIImagePicker
: 这是我提供
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)
To detect access to your library: 要检测对库的访问:
You need to use AssetsLibrary for that. 您需要使用AssetsLibrary。 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)
})
}
To detect access to camera: 要检测对相机的访问:
You need to use AVFoundation for that. 你需要使用AVFoundation。 First, import avfoundation framework:
一,导入avfoundation框架:
import AVFoundation
Then, as previously, request user permission when you go to imagepicker and catch the event. 然后,如前所述,当您转到imagepicker并捕获事件时请求用户权限。
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)
}
})
}
Hope it helps! 希望能帮助到你!
In iOS 10, use: 在iOS 10中,使用:
import Photos
let authStatus = PHPhotoLibrary.authorizationStatus()
if authStatus == .notDetermined || authStatus == .denied {
PHPhotoLibrary.requestAuthorization({ (status) in
if status == PHAuthorizationStatus.authorized {
} else {
imagePickerController.dismissViewControllerAnimated(true, completion: nil)
}
})
}
Check out this for detecting camera permission 看看这个用于检测相机许可
Presenting camera permission dialog in iOS 8 在iOS 8中显示相机权限对话框
Use this when user picks Don't Allow. 用户选择“不允许”时使用此选项。
PhotoPicker.dismissViewControllerAnimated(false, completion: nil)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.