From 2f38cefcf210ef55493b4be271c6017afb3a41ff Mon Sep 17 00:00:00 2001 From: mlch911 Date: Mon, 23 Sep 2024 17:51:42 +0800 Subject: [PATCH] Add support for simulator --- Source/iOS/Device.swift | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/Source/iOS/Device.swift b/Source/iOS/Device.swift index 68ed95c..955baa5 100644 --- a/Source/iOS/Device.swift +++ b/Source/iOS/Device.swift @@ -10,7 +10,16 @@ import UIKit open class Device { - static fileprivate func getVersionCode() -> String { + /// Get Version Code of the device. + /// - Parameter detectSimulator: if true, reture the Version Code like real device. Otherwise return Simulator. + /// - Returns: Version Code + static fileprivate func getVersionCode(detectSimulator: Bool = false) -> String { +#if targetEnvironment(simulator) + if detectSimulator { + return ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"]! + } +#endif + var systemInfo = utsname() uname(&systemInfo) @@ -128,8 +137,8 @@ open class Device { } } - static fileprivate func getType(code: String) -> Type { - let versionCode = getVersionCode() + static fileprivate func getType(code: String, detectSimulator: Bool) -> Type { + let versionCode = getVersionCode(detectSimulator: detectSimulator) if versionCode.contains("iPhone") { return .iPhone @@ -144,8 +153,8 @@ open class Device { } } - static public func version() -> Version { - return getVersion(code: getVersionCode()) + static public func version(detectSimulator: Bool = false) -> Version { + return getVersion(code: getVersionCode(detectSimulator: detectSimulator)) } static public func size() -> Size { @@ -214,8 +223,8 @@ open class Device { } } - static public func type() -> Type { - return getType(code: getVersionCode()) + static public func type(detectSimulator: Bool = false) -> Type { + return getType(code: getVersionCode(detectSimulator: detectSimulator), detectSimulator: detectSimulator) } @available(*, deprecated, message: "use == operator instead") @@ -237,15 +246,11 @@ open class Device { return UIScreen.main.scale > 1.0 } - static public func isPad() -> Bool { - return type() == .iPad - } - - static public func isPhone() -> Bool { - return type() == .iPhone + static public func isPad(detectSimulator: Bool = true) -> Bool { + return type(detectSimulator: detectSimulator) == .iPad } - static public func isPod() -> Bool { + static public func isPod(detectSimulator: Bool = true) -> Bool { return type() == .iPod } @@ -258,7 +263,7 @@ open class Device { // MARK: - Dynamic island extension Device { static public var hasDynamicIsland: Bool { - switch version() { + switch version(detectSimulator: true) { case .iPhone14Pro, .iPhone14Pro_Max, .iPhone15,