Skip to content

Commit 36e221b

Browse files
authored
Add support for simulator (#139)
1 parent 628fa2e commit 36e221b

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

Source/iOS/Device.swift

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,16 @@
1010
import UIKit
1111

1212
open class Device {
13-
static fileprivate func getVersionCode() -> String {
13+
/// Get Version Code of the device.
14+
/// - Parameter detectSimulator: if true, reture the Version Code like real device. Otherwise return Simulator.
15+
/// - Returns: Version Code
16+
static fileprivate func getVersionCode(detectSimulator: Bool = false) -> String {
17+
#if targetEnvironment(simulator)
18+
if detectSimulator {
19+
return ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"]!
20+
}
21+
#endif
22+
1423
var systemInfo = utsname()
1524
uname(&systemInfo)
1625

@@ -133,8 +142,8 @@ open class Device {
133142
}
134143
}
135144

136-
static fileprivate func getType(code: String) -> Type {
137-
let versionCode = getVersionCode()
145+
static fileprivate func getType(code: String, detectSimulator: Bool) -> Type {
146+
let versionCode = getVersionCode(detectSimulator: detectSimulator)
138147

139148
if versionCode.contains("iPhone") {
140149
return .iPhone
@@ -149,8 +158,8 @@ open class Device {
149158
}
150159
}
151160

152-
static public func version() -> Version {
153-
return getVersion(code: getVersionCode())
161+
static public func version(detectSimulator: Bool = false) -> Version {
162+
return getVersion(code: getVersionCode(detectSimulator: detectSimulator))
154163
}
155164

156165
static public func size() -> Size {
@@ -221,8 +230,8 @@ open class Device {
221230
}
222231
}
223232

224-
static public func type() -> Type {
225-
return getType(code: getVersionCode())
233+
static public func type(detectSimulator: Bool = false) -> Type {
234+
return getType(code: getVersionCode(detectSimulator: detectSimulator), detectSimulator: detectSimulator)
226235
}
227236

228237
@available(*, deprecated, message: "use == operator instead")
@@ -244,15 +253,11 @@ open class Device {
244253
return UIScreen.main.scale > 1.0
245254
}
246255

247-
static public func isPad() -> Bool {
248-
return type() == .iPad
249-
}
250-
251-
static public func isPhone() -> Bool {
252-
return type() == .iPhone
256+
static public func isPad(detectSimulator: Bool = true) -> Bool {
257+
return type(detectSimulator: detectSimulator) == .iPad
253258
}
254259

255-
static public func isPod() -> Bool {
260+
static public func isPod(detectSimulator: Bool = true) -> Bool {
256261
return type() == .iPod
257262
}
258263

@@ -265,7 +270,7 @@ open class Device {
265270
// MARK: - Dynamic island
266271
extension Device {
267272
static public var hasDynamicIsland: Bool {
268-
switch version() {
273+
switch version(detectSimulator: true) {
269274
case .iPhone14Pro,
270275
.iPhone14Pro_Max,
271276
.iPhone15,

0 commit comments

Comments
 (0)