From 9e20473decfdc8085f978a5239c998bc4faf522d Mon Sep 17 00:00:00 2001 From: Michael Shamash Date: Fri, 16 Jul 2021 18:04:38 -0400 Subject: [PATCH] Added code to check camera access --- OnePetri.xcodeproj/project.pbxproj | 4 +- OnePetri/Base.lproj/Main.storyboard | 27 ++++++++++--- .../Controllers/MainMenuViewController.swift | 38 +++++++++++++++++-- .../SelectImageViewController.swift | 37 ++++++++++++++++-- 4 files changed, 92 insertions(+), 14 deletions(-) diff --git a/OnePetri.xcodeproj/project.pbxproj b/OnePetri.xcodeproj/project.pbxproj index f5dbde0..07d1746 100644 --- a/OnePetri.xcodeproj/project.pbxproj +++ b/OnePetri.xcodeproj/project.pbxproj @@ -359,7 +359,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 2; + CURRENT_PROJECT_VERSION = 3; DEVELOPMENT_TEAM = H7C3AE5HUF; INFOPLIST_FILE = OnePetri/Info.plist; LD_RUNPATH_SEARCH_PATHS = ( @@ -380,7 +380,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 2; + CURRENT_PROJECT_VERSION = 3; DEVELOPMENT_TEAM = H7C3AE5HUF; INFOPLIST_FILE = OnePetri/Info.plist; LD_RUNPATH_SEARCH_PATHS = ( diff --git a/OnePetri/Base.lproj/Main.storyboard b/OnePetri/Base.lproj/Main.storyboard index 19d5e60..a9d18e1 100644 --- a/OnePetri/Base.lproj/Main.storyboard +++ b/OnePetri/Base.lproj/Main.storyboard @@ -229,16 +229,16 @@ - + - + + + diff --git a/OnePetri/Controllers/MainMenuViewController.swift b/OnePetri/Controllers/MainMenuViewController.swift index 2d9ab30..6bbd2e7 100644 --- a/OnePetri/Controllers/MainMenuViewController.swift +++ b/OnePetri/Controllers/MainMenuViewController.swift @@ -6,6 +6,7 @@ // import UIKit +import AVFoundation enum Assay { case quick, plaque, adsorption, eop } @@ -44,11 +45,42 @@ class MainMenuViewController: UIViewController { picker.delegate = self if sender.tag == 0 { picker.sourceType = .photoLibrary + picker.modalPresentationStyle = .overFullScreen + present(picker, animated: true) } else if sender.tag == 1 { - picker.sourceType = .camera + if AVCaptureDevice.authorizationStatus(for: .video) == .authorized { + //already authorized + picker.sourceType = .camera + picker.modalPresentationStyle = .overFullScreen + present(picker, animated: true) + print("auth") + } else { + AVCaptureDevice.requestAccess(for: .video, completionHandler: { (granted: Bool) in + if granted { + //access allowed + print("granted") + DispatchQueue.main.async { + picker.sourceType = .camera + picker.modalPresentationStyle = .overFullScreen + self.present(picker, animated: true) + } + } else { + //access denied + DispatchQueue.main.async { + let alert = UIAlertController(title: "Camera access disabled", message: "It looks like camera access for OnePetri has been disabled. Please enable access in iOS Settings if you wish to use the camera to take photos for analysis within OnePetri.", preferredStyle: .alert) + + alert.addAction(UIAlertAction(title: "Open Settings", style: .default, handler: { _ in + UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!) + })) + alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil)) + + self.present(alert, animated: true) + } + } + }) + } } - picker.modalPresentationStyle = .overFullScreen - present(picker, animated: true) + } @IBAction func didChooseAssay(_ sender: UIButton) { diff --git a/OnePetri/Controllers/SelectImageViewController.swift b/OnePetri/Controllers/SelectImageViewController.swift index 68654fd..8f5ef96 100644 --- a/OnePetri/Controllers/SelectImageViewController.swift +++ b/OnePetri/Controllers/SelectImageViewController.swift @@ -7,6 +7,7 @@ import UIKit import Vision +import AVFoundation class SelectImageViewController: UIViewController { @IBOutlet weak var photoLibraryButton: UIButton! @@ -124,11 +125,41 @@ class SelectImageViewController: UIViewController { picker.delegate = self if sender.tag == 0 { picker.sourceType = .photoLibrary + picker.modalPresentationStyle = .overFullScreen + present(picker, animated: true) } else if sender.tag == 1 { - picker.sourceType = .camera + if AVCaptureDevice.authorizationStatus(for: .video) == .authorized { + //already authorized + picker.sourceType = .camera + picker.modalPresentationStyle = .overFullScreen + present(picker, animated: true) + print("auth") + } else { + AVCaptureDevice.requestAccess(for: .video, completionHandler: { (granted: Bool) in + if granted { + //access allowed + print("granted") + DispatchQueue.main.async { + picker.sourceType = .camera + picker.modalPresentationStyle = .overFullScreen + self.present(picker, animated: true) + } + } else { + //access denied + DispatchQueue.main.async { + let alert = UIAlertController(title: "Camera access disabled", message: "It looks like camera access for OnePetri has been disabled. Please enable access in iOS Settings if you wish to use the camera to take photos for analysis within OnePetri.", preferredStyle: .alert) + + alert.addAction(UIAlertAction(title: "Open Settings", style: .default, handler: { _ in + UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!) + })) + alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil)) + + self.present(alert, animated: true) + } + } + }) + } } - picker.modalPresentationStyle = .overFullScreen - present(picker, animated: true) } @discardableResult