Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for simulator #139

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 20 additions & 15 deletions Source/iOS/Device.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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")
Expand All @@ -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
}

Expand All @@ -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,
Expand Down