Skip to content

Instantly share code, notes, and snippets.

@therealFoxster
Last active August 18, 2022 09:27
Show Gist options
  • Save therealFoxster/ae2d6f113ed7a965ec0f6990ff3b8099 to your computer and use it in GitHub Desktop.
Save therealFoxster/ae2d6f113ed7a965ec0f6990ff3b8099 to your computer and use it in GitHub Desktop.
import Foundation
import UIKit
import Photos
class HBTools {
static let appName = Bundle.main.object(forInfoDictionaryKey: "CFBundleName") as? String ?? "String_appName"
enum Orientation {
case portrait
case landscape
}
static func getScreenDimensions(for orientation: Orientation = .portrait) -> CGSize {
var width = UIScreen.main.bounds.width,
height = UIScreen.main.bounds.height
switch orientation {
case .portrait: // Height > width
if width > height {
width = height
height = UIScreen.main.bounds.width
}
case .landscape: // Width > height
if height > width {
height = width
width = UIScreen.main.bounds.height
}
}
return CGSize(width: width, height: height)
}
static func doNothing() {}
static func presentImagePicker(_ picker: UIImagePickerController, fromViewController viewController: UIViewController) {
switch PHPhotoLibrary.authorizationStatus() {
case .authorized:
viewController.present(picker, animated: true)
case .notDetermined:
PHPhotoLibrary.requestAuthorization(for: .readWrite) {
[weak viewController] newStatus in
if newStatus == PHAuthorizationStatus.authorized {
DispatchQueue.main.async {
viewController?.present(picker, animated: true)
}
}
}
case .limited: // The user authorized this app for limited photo library access
fallthrough
case .denied: // The user explicitly denied this app access to the photo library
let title = "\"\(appName)\" Would Like to Access Your Photos",
message = Bundle.main.object(forInfoDictionaryKey: "NSPhotoLibraryUsageDescription") as? String
let alert = getAlert(title: title, message: message, cancelActionTitle: "Don't Allow", primaryActionTitle: "Allow Full Access in Settings") {
_ in openApplicationSettingsPane()
}
viewController.present(alert, animated: true)
case .restricted: // The app isn’t authorized to access the photo library, and the user can’t grant such permission
let title = "\"\(appName)\" Was Unable to Access to Your Photos",
message = "This app requires permission to access to your photos. However, it seems like your device is in a restricted state and unable to grant such permission."
let alert = getAlert(title: title, message: message, cancelActionTitle: "OK")
viewController.present(alert, animated: true)
@unknown default:
fatalError("Unable to determine user's photo library authorization status.")
}
}
static func log(_ items: Any) {
print("[log] \(getDateString(withFormat: "dd-MM-YYYY hh:mm:ss")) \(items)")
}
static func openApplicationSettingsPane() {
UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!)
}
static func getAlert(title: String? = nil, message: String?, cancelActionTitle: String = "Cancel", primaryActionTitle: String = "Continue", primaryAction: ((UIAlertAction) -> Void)? = nil) -> UIAlertController {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: cancelActionTitle, style: .cancel))
if primaryAction != nil {
alert.addAction(UIAlertAction(title: primaryActionTitle, style: .default, handler: primaryAction))
}
return alert
}
static func getDateString(fromDate date: Date = Date(), withFormat format: String) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = format
return dateFormatter.string(from: date)
}
// Thanks to @TwoStraws
static func getDocumentsDirectory() -> URL {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
return paths[0]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment