Swift common constants and methods

import UIKit

// MARK: - Interface configuration is related

/// Whether it is a test environment
let isTest = true

Url / / / interface
let testHost = "http://192.168.1:8081/"
let formalHost = "http://192.168.2:8081/"
let hostString = (isTest ? testHost : formalHost)

Url / / / resources
let testResourceHost = "http://192.168.3:8081"
let formalResourceHost = "http://192.168.4:8081"
let resourceHost = isTest ? testResourceHost : formalResourceHost


// MARK: - Screen size is relevant

/ / / the screen width
let screenWidth = UIScreen.main.bounds.size.width

/ / / screen
let screentHeight = UIScreen.main.bounds.size.height

/// bottom safe distance
let bottomSafeAreaHeight = UIApplication.shared.windows.first?.safeAreaInsets.bottom ?? 0.0

/// The top of the safe distance
let topSafeAreaHeight = UIApplication.shared.windows.first?.safeAreaInsets.top ?? 0.0

/// Status bar height
let statusBarHeight = getStatusBarHeight()
func getStatusBarHeight(a) -> CGFloat {
    var statusBarHeight: CGFloat = 0.0
    if #available(iOS 13.0.*) {let statusBarManager:UIStatusBarManager = UIApplication.shared.windows.first!.windowScene!.statusBarManager!
        statusBarHeight = statusBarManager.statusBarFrame.size.height
    }
    else {
        statusBarHeight = UIApplication.shared.statusBarFrame.height
    }
    return statusBarHeight
}


// MARK: - Color
func hexColor(_ hexColor: Int) -> UIColor! {
    let color = UIColor(red: ((CGFloat)((hexColor & 0xFF0000) >> 16)) / 255.0,
                        green: ((CGFloat)((hexColor & 0xFF00) >> 8)) / 255.0,
                        blue: ((CGFloat)(hexColor & 0xFF)) / 255.0,alpha: 1.0)
    
    
    return color
}

func hexColor(hexColor: Int.alpha: Float) -> UIColor! {
    return UIColor(red: ((CGFloat)((hexColor & 0xFF0000) >> 16)) / 255.0,
                   green: ((CGFloat)((hexColor & 0xFF00) >> 8)) / 255.0,
                   blue: ((CGFloat)(hexColor & 0xFF)) / 255.0,alpha: CGFloat(alpha))
}


// MARK: - Time

// get the current timestamp (in seconds)
func getNowTimeStamp(a) -> String {
    // Get the current time
    let now = Date(a)// Timestamp of the current time
    let timeInterval:TimeInterval = now.timeIntervalSince1970
    let timeStamp = Int(timeInterval)
    return "\(timeStamp)"
}

/// Get the current time
/// -parameter dateFormat: time format (e.g. Yyyy-mm-dd HH: MM :ss)
func getNowTimeString(dateFormat: String) -> String {
    // Get the current time
    let now = Date(a)// Create a date formatter
    let dformatter = DateFormatter()
    dformatter.dateFormat = dateFormat
    return dformatter.string(from: now)
}


// MARK: - Other
/ / / screenshots
func screenSnapshot(a) -> UIImage? {
    guard UIScreen.main.bounds.size.height > 0 && UIScreen.main.bounds.size.width > 0 else {
        return nil
    }
    UIGraphicsBeginImageContextWithOptions(UIScreen.main.bounds.size, true.UIScreen.main.scale)
    
    var window: UIWindow? = nil
    if #available(iOS 13.0.*) {
        window = UIApplication.shared.connectedScenes
            .filter({ $0.activationState = = .foregroundActive })
            .map({ $0 as? UIWindowScene })
            .compactMap({ $0 })
            .last?.windows
            .filter({ $0.isKeyWindow })
            .last
    } else {
        window = UIApplication.shared.keyWindow
    }
    if window = = nil {
        return nil
    }
    window!.drawHierarchy(in: window!.bounds, afterScreenUpdates: true)  // Hd screenshot
    let image = UIGraphicsGetImageFromCurrentImageContext(a)UIGraphicsEndImageContext(a)return image
}

Copy the code

Demo: Github