SWIFT
Como Juan Boero escribió comprobar el:
if UIImagePickerController.isSourceTypeAvailable(.camera){ }
pero me gustaría añadir otra comprobación para ver si el usuario permite el acceso a la cámara como manzana sugiere en su ejemplo PhotoPicker (PhotoPicker example Objective-C):
* tenga en cuenta que tiene que importar AVFoundation
let authStatus = AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo)
if authStatus == AVAuthorizationStatus.denied {
// Denied access to camera
// Explain that we need camera access and how to change it.
let dialog = UIAlertController(title: "Unable to access the Camera", message: "To enable access, go to Settings > Privacy > Camera and turn on Camera access for this app.", preferredStyle: UIAlertControllerStyle.alert)
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)
dialog.addAction(okAction)
self.present(dialog, animated:true, completion:nil)
} else if authStatus == AVAuthorizationStatus.notDetermined { // The user has not yet been presented with the option to grant access to the camera hardware.
// Ask for it.
AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo, completionHandler: { (grantd) in
// If access was denied, we do not set the setup error message since access was just denied.
if grantd {
// Allowed access to camera, go ahead and present the UIImagePickerController.
self.showImagePickerForSourceType(sourceType: UIImagePickerControllerSourceType.camera)
}
})
} else {
// Allowed access to camera, go ahead and present the UIImagePickerController.
self.showImagePickerForSourceType(sourceType: UIImagePickerControllerSourceType.camera)
}
func showImagePickerForSourceType(sourceType: UIImagePickerControllerSourceType) {
let myPickerController = UIImagePickerController()
myPickerController.delegate = self;
myPickerController.sourceType = sourceType
self.present(myPickerController, animated: true, completion: nil)
}
Versión Swift: [Enlace] (http://stackoverflow.com/a/33697919/1634890) –