我正在使用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)
答案 0 :(得分:5)
检测对您图书馆的访问权限:
您需要使用AssetsLibrary。首先,导入资产库框架:
import AssetsLibrary
然后,请求授权状态,如果未确定,则使用块来捕获这些事件,如下所示:
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框架:
import AVFoundation
然后,如前所述,当您转到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)
}
})
}
希望它有所帮助!
答案 1 :(得分:3)
在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)
}
})
}
答案 2 :(得分:0)
检查此项以检测相机许可
Presenting camera permission dialog in iOS 8
当用户选择“不允许”时使用此功能。
PhotoPicker.dismissViewControllerAnimated(false, completion: nil)