/AVFoundationHelper.swift Secret
Created
November 1, 2020 11:10
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import AVFoundation | |
enum CameraStatus { | |
case notDetermined | |
case restricted | |
case denied | |
case authorized | |
} | |
protocol AVFoundationHelperProtocol: AnyObject { | |
// MARK: - Check and Respond to Camera Authorization Status | |
var authorizationStatus: CameraStatus { get } | |
// MARK: - Request Camera Permission | |
func requestAccess(completionHandler handler: @escaping (Bool) -> Void) | |
} | |
final class AVFoundationHelper: AVFoundationHelperProtocol { | |
// MARK: - Check and Respond to Camera Authorization Status | |
var authorizationStatus: CameraStatus { | |
let cameraAuthorizationStatus = AVCaptureDevice.authorizationStatus(for: .video) | |
switch cameraAuthorizationStatus { | |
case .notDetermined: | |
return CameraStatus.notDetermined | |
case .authorized: | |
return CameraStatus.authorized | |
case .restricted: | |
return CameraStatus.restricted | |
case .denied: | |
return CameraStatus.denied | |
@unknown default: | |
return CameraStatus.notDetermined | |
} | |
} | |
// MARK: - Request Camera Permission | |
func requestAccess(completionHandler handler: @escaping (Bool) -> Void) { | |
AVCaptureDevice.requestAccess(for: .video, completionHandler: { accessGranted in | |
handler(accessGranted) | |
}) | |
} | |
} | |
import UIKit | |
final class ViewController: UIViewController { | |
@IBOutlet var cameraAccessButton: UIButton! | |
@IBOutlet var photoImageView: UIImageView! | |
private var model: AVFoundationHelperProtocol = AVFoundationHelper() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
} | |
@IBAction func cameraButtonPressed(_: Any) { | |
let status = model.authorizationStatus | |
switch status { | |
case .notDetermined: | |
model.requestAccess { hasAccess in | |
if hasAccess { | |
DispatchQueue.main.async { | |
self.showCameraReader() | |
} | |
} else { | |
self.alertCameraAccessNeeded() | |
} | |
} | |
case .restricted, .denied: | |
alertCameraAccessNeeded() | |
case .authorized: | |
showCameraReader() | |
} | |
} | |
private func alertCameraAccessNeeded() { | |
let appName = "This app Name" | |
let alert = UIAlertController(title: "This feature requires Camera Access", | |
message: "In iPhone settings, tap \(appName) and turn on Camera access", | |
preferredStyle: UIAlertController.Style.alert) | |
let actionSettings = UIAlertAction(title: "Settings", style: .default, handler: { _ -> Void in | |
guard let settingsAppURL = URL(string: UIApplication.openSettingsURLString) else { return } | |
UIApplication.shared.open(settingsAppURL) | |
}) | |
let actionCancel = UIAlertAction(title: "Cancel", style: .destructive, handler: { _ -> Void in | |
}) | |
alert.addAction(actionSettings) | |
alert.addAction(actionCancel) | |
present(alert, animated: true, completion: nil) | |
} | |
} | |
extension ViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate { | |
private func showCameraReader() { | |
if UIImagePickerController.isSourceTypeAvailable(.camera) { | |
let imagePicker = UIImagePickerController() | |
imagePicker.sourceType = .camera | |
imagePicker.allowsEditing = true | |
imagePicker.delegate = self | |
present(imagePicker, animated: true) | |
} else if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) { | |
let imagePicker = UIImagePickerController() | |
imagePicker.sourceType = .photoLibrary | |
imagePicker.allowsEditing = true | |
imagePicker.delegate = self | |
present(imagePicker, animated: true) | |
} else { | |
// TODO: Implement proper alert | |
alertCameraAccessNeeded() | |
} | |
} | |
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) { | |
picker.dismiss(animated: true) | |
guard let image = info[.editedImage] as? UIImage else { | |
print("No image found") | |
return | |
} | |
photoImageView.image = image | |
// print out the image size as a test | |
print(image.size) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment