Skip to content

Instantly share code, notes, and snippets.

@NeilsUltimateLab
Created February 3, 2019 08:04
Show Gist options
  • Select an option

  • Save NeilsUltimateLab/1fc0f1d907e8f4f78b0f4456d857e207 to your computer and use it in GitHub Desktop.

Select an option

Save NeilsUltimateLab/1fc0f1d907e8f4f78b0f4456d857e207 to your computer and use it in GitHub Desktop.
Image Picker Displaying Protocol and UIViewController extension for Image Picker.
import UIKit
import AVFoundation
import Photos
protocol ImagePickerDiplaying: class {
func pickerAction(sourceType : UIImagePickerControllerSourceType)
func alertForPermissionChange(forFeature feature: String, library: String, action: String)
func cameraAccessPermissionCheck(completion: @escaping (Bool) -> Void)
func photosAccessPermissionCheck(completion: @escaping (Bool)->Void)
}
extension ImagePickerDiplaying where Self: UIViewController & UIImagePickerControllerDelegate & UINavigationControllerDelegate {
func showMediaPickerOptions() {
let fromCameraAction = UIAlertAction(title: "Capture photo from camera", style: .default) { (_) in
self.pickerAction(sourceType: .camera)
}
let fromPhotoLibraryAction = UIAlertAction(title: "Select from photo library", style: .default) { (_) in
self.pickerAction(sourceType: .photoLibrary)
}
let cancelAction = UIAlertAction.cancel()
let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
if UIImagePickerController.isSourceTypeAvailable(.camera) {
alert.addAction(fromCameraAction)
}
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
alert.addAction(fromPhotoLibraryAction)
}
alert.addAction(cancelAction)
self.showAlert(alert)
}
func pickerAction(sourceType : UIImagePickerControllerSourceType) {
if UIImagePickerController.isSourceTypeAvailable(sourceType) {
let picker = UIImagePickerController()
picker.sourceType = sourceType
picker.delegate = self
if sourceType == .camera {
self.cameraAccessPermissionCheck(completion: { (success) in
if success {
self.present(picker, animated: true, completion: nil)
}else {
self.alertForPermissionChange(forFeature: "Camera", library: "Camera", action: "take")
}
})
}
if sourceType == .photoLibrary {
self.photosAccessPermissionCheck(completion: { (success) in
if success {
self.present(picker, animated: true, completion: nil)
}else {
self.alertForPermissionChange(forFeature: "Photos", library: "Photo Library", action: "select")
}
})
}
}
}
func alertForPermissionChange(forFeature feature: String, library: String, action: String) {
let settingsAction = UIAlertAction(title: "Open Settings", style: .default) { (_) in
UIApplication.shared.openSettings()
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
// Please enable camera access from Settings > reiwa.com > Camera to take photos
let appName = Bundle.main.infoDictionary?["CFBundleDisplayName"] as? String ?? "App"
self.showAlert(title: "\"\(appName)\" Would Like to Access the \(library)",
message: "Please enable \(library) access from Settings > \(appName) > \(feature) to \(action) photos",
actions: settingsAction, cancelAction)
}
func cameraAccessPermissionCheck(completion: @escaping (Bool) -> Void) {
let cameraMediaType = AVMediaType.video
let cameraAutherisationState = AVCaptureDevice.authorizationStatus(for: cameraMediaType)
switch cameraAutherisationState {
case .authorized:
completion(true)
case .denied, .notDetermined, .restricted:
AVCaptureDevice.requestAccess(for: cameraMediaType, completionHandler: { (granted) in
DispatchQueue.main.async {
completion(granted)
}
})
}
}
func photosAccessPermissionCheck(completion: @escaping (Bool)->Void) {
let photosStatus = PHPhotoLibrary.authorizationStatus()
switch photosStatus {
case .authorized:
completion(true)
case .denied, .notDetermined, .restricted:
PHPhotoLibrary.requestAuthorization({ (status) in
DispatchQueue.main.async {
switch status {
case .authorized:
completion(true)
default:
completion(false)
}
}
})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment