Skip to content

Instantly share code, notes, and snippets.

@richardwei6
Last active December 10, 2021 18:04
Show Gist options
  • Save richardwei6/45bf91a8f8f7ad5fd7410790ee86908c to your computer and use it in GitHub Desktop.
Save richardwei6/45bf91a8f8f7ad5fd7410790ee86908c to your computer and use it in GitHub Desktop.
//
// AppUtility.swift
// AHS
//
// Created by Richard Wei.
//
import Foundation
import UIKit
struct AppUtility {
static let originalWidth = UIScreen.main.bounds.width;
static let originalHeight = UIScreen.main.bounds.height;
static let safeAreaInset = UIApplication.shared.windows[0].safeAreaInsets;
static private var isOrientationLocked = false;
static private var isLockedOrientationLandscape = false;
static public var currentUserInterfaceStyle : UIUserInterfaceStyle = .unspecified; // used for overriding system user interface style, use system style when .unspecified
static func lockOrientation(_ orientation: UIInterfaceOrientationMask) {
if let delegate = UIApplication.shared.delegate as? AppDelegate {
delegate.orientationLock = orientation;
}
isOrientationLocked = orientation != .all;
isLockedOrientationLandscape = UIDevice.current.orientation.isLandscape;
}
/// OPTIONAL Added method to adjust lock and rotate to the desired orientation
static func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo rotateOrientation:UIInterfaceOrientation) {
self.lockOrientation(orientation);
UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")
UINavigationController.attemptRotationToDeviceOrientation();
isLockedOrientationLandscape = rotateOrientation.isLandscape;
}
static func getCurrentScreenSize() -> CGSize{
let isLandscape = isOrientationLocked ? isLockedOrientationLandscape : UIDevice.current.orientation.isLandscape;
return CGSize(width: (isLandscape ? AppUtility.originalHeight : AppUtility.originalWidth), height: (isLandscape ? AppUtility.originalWidth : AppUtility.originalHeight));
}
//
static func setAppNotificationNumber(_ n: Int){
UIApplication.shared.applicationIconBadgeNumber = n;
}
static func getAppNotificationNumber() -> Int{
return UIApplication.shared.applicationIconBadgeNumber;
}
//
static func getAppVersionString() -> String{
return Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "1.0";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment