Skip to content

Commit

Permalink
Added code to check camera access
Browse files Browse the repository at this point in the history
  • Loading branch information
mshamash committed Jul 16, 2021
1 parent d69df8d commit 9e20473
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 14 deletions.
4 changes: 2 additions & 2 deletions OnePetri.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand All @@ -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 = (
Expand Down
27 changes: 21 additions & 6 deletions OnePetri/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -229,16 +229,16 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" alignment="center" spacing="8" translatesAutoresizingMaskIntoConstraints="NO" id="yw2-d1-srA">
<rect key="frame" x="129.66666666666666" y="413.33333333333331" width="168.99999999999997" height="199.66666666666669"/>
<rect key="frame" x="118" y="390.66666666666674" width="192" height="245"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Quick count:" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="T16-Mk-Qsp">
<rect key="frame" x="24.333333333333343" y="0.0" width="120" height="26.333333333333332"/>
<rect key="frame" x="36" y="0.0" width="120" height="26.333333333333332"/>
<fontDescription key="fontDescription" type="system" pointSize="22"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<stackView opaque="NO" contentMode="scaleToFill" spacing="30" translatesAutoresizingMaskIntoConstraints="NO" id="3cf-05-fQB">
<rect key="frame" x="19.333333333333343" y="34.333333333333371" width="130" height="50"/>
<rect key="frame" x="31" y="34.333333333333314" width="130" height="50"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="gTm-Ev-waW">
<rect key="frame" x="0.0" y="0.0" width="50" height="50"/>
Expand Down Expand Up @@ -285,7 +285,7 @@
</subviews>
</stackView>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text=" " textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="hcy-5L-Vc3">
<rect key="frame" x="82" y="92.333333333333371" width="4.6666666666666714" height="10"/>
<rect key="frame" x="93.666666666666657" y="92.333333333333314" width="4.6666666666666714" height="10"/>
<constraints>
<constraint firstAttribute="height" constant="10" id="Gzy-lP-5Ge"/>
</constraints>
Expand All @@ -294,13 +294,13 @@
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Perform an assay:" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="qGR-8e-0r5">
<rect key="frame" x="0.0" y="110.33333333333331" width="169" height="26.333333333333343"/>
<rect key="frame" x="11.666666666666657" y="110.33333333333331" width="169" height="26.333333333333343"/>
<fontDescription key="fontDescription" type="system" pointSize="22"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<button opaque="NO" tag="1" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="n25-nH-etE">
<rect key="frame" x="7" y="144.66666666666669" width="155" height="55"/>
<rect key="frame" x="18.666666666666657" y="144.66666666666669" width="155" height="55"/>
<color key="backgroundColor" red="0.3529411764705882" green="0.20000000000000001" blue="0.55294117647058827" alpha="1" colorSpace="custom" customColorSpace="displayP3"/>
<constraints>
<constraint firstAttribute="height" constant="55" id="2KE-tz-OKA"/>
Expand Down Expand Up @@ -359,6 +359,21 @@
<action selector="didChooseAssay:" destination="kxU-si-2fA" eventType="touchUpInside" id="jGZ-Kr-08q"/>
</connections>
</button>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text=" " textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="oZj-rV-efd">
<rect key="frame" x="93.666666666666657" y="207.66666666666669" width="4.6666666666666714" height="10"/>
<constraints>
<constraint firstAttribute="height" constant="10" id="14k-ug-5U8"/>
</constraints>
<fontDescription key="fontDescription" type="italicSystem" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="More assays coming soon!" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Eu6-ah-NjY">
<rect key="frame" x="0.0" y="225.66666666666669" width="192" height="19.333333333333343"/>
<fontDescription key="fontDescription" type="italicSystem" pointSize="16"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<variation key="default">
<mask key="subviews">
Expand Down
38 changes: 35 additions & 3 deletions OnePetri/Controllers/MainMenuViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import UIKit
import AVFoundation

enum Assay { case quick, plaque, adsorption, eop }

Expand Down Expand Up @@ -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) {
Expand Down
37 changes: 34 additions & 3 deletions OnePetri/Controllers/SelectImageViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import UIKit
import Vision
import AVFoundation

class SelectImageViewController: UIViewController {
@IBOutlet weak var photoLibraryButton: UIButton!
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9e20473

Please sign in to comment.