diff --git a/Sources/DeviceFamily.swift b/Sources/DeviceFamily.swift index fece165..545ede2 100644 --- a/Sources/DeviceFamily.swift +++ b/Sources/DeviceFamily.swift @@ -26,7 +26,6 @@ public enum DeviceFamily: String { case iPhone case iPod case iPad - case simulator case unknown public init(rawValue: String) { @@ -37,10 +36,20 @@ public enum DeviceFamily: String { self = .iPod case "iPad": self = .iPad - case "x86_64", "i386": - self = .simulator default: self = .unknown } } } + +// MARK: Simulator Detection + +extension DeviceFamily { + public var isSimulator: Bool { + #if arch(i386) || arch(x86_64) + return true + #else + return false + #endif + } +} diff --git a/Sources/DeviceModel.swift b/Sources/DeviceModel.swift index d736188..b471cdf 100644 --- a/Sources/DeviceModel.swift +++ b/Sources/DeviceModel.swift @@ -64,8 +64,6 @@ extension DeviceModel { self = DeviceModel.detectIpadModel(with: identifier) case .iPod: self = DeviceModel.detectIpodModel(with: identifier) - case .simulator: - self = DeviceModel.detectSimulatorModel() default: self = .unknown } @@ -170,50 +168,6 @@ extension DeviceModel { } -// MARK: Detecting Simulator Models - -extension DeviceModel { - fileprivate static func detectSimulatorModel() -> DeviceModel { - guard let versionInfo = ProcessInfo.processInfo.environment["SIMULATOR_VERSION_INFO"], - let startIdx = versionInfo.range(of: "DeviceType: ")?.upperBound else { - return .unknown - } - let deviceType = versionInfo[startIdx.. (type: String, major: Int?, minor: Int?) { - // Simulator Check - if identifierString == "x86_64" || identifierString == "i386" { - return (identifierString, nil, nil) - } - let numericCharacters: [String] = (0...9).map { "\($0)" } let type = identifierString.prefix(while: { !numericCharacters.contains(String($0))}) @@ -74,8 +69,6 @@ extension Identifier: CustomStringConvertible { return iPadStringRepresentation(major: major, minor: minor) case .iPod: return iPodStringRepresentation(major: major, minor: minor) - case .simulator: - return "Simulator" case .unknown: return "unknown" } diff --git a/Sources/System.swift b/Sources/System.swift index 0fb1585..cecd852 100644 --- a/Sources/System.swift +++ b/Sources/System.swift @@ -31,8 +31,17 @@ class System { // and the rest of the string being zeroed out. let encoding: UInt = String.Encoding.ascii.rawValue if let string = NSString(bytes: &systemInfo.machine, length: Int(_SYS_NAMELEN), encoding: encoding) { - return (string as String).components(separatedBy: "\0").first + let identifier = (string as String).components(separatedBy: "\0").first + + // Simulator Check + if identifier == "x86_64" || identifier == "i386" { + return ProcessInfo.processInfo.environment["SIMULATOR_MODEL_IDENTIFIER"] + } + + return identifier } + + return nil } } diff --git a/Sources/UIDeviceExtensions.swift b/Sources/UIDeviceExtensions.swift index 9c5a1e5..e95c2a5 100644 --- a/Sources/UIDeviceExtensions.swift +++ b/Sources/UIDeviceExtensions.swift @@ -61,10 +61,6 @@ public extension UIDeviceComplete where Base == UIDevice { return deviceFamily == .iPod } - /// Simulator - var isSimulator: Bool { - return deviceFamily == .simulator - } } diff --git a/Tests/DeviceFamilyTests.swift b/Tests/DeviceFamilyTests.swift index 363daf9..8a83d79 100644 --- a/Tests/DeviceFamilyTests.swift +++ b/Tests/DeviceFamilyTests.swift @@ -40,9 +40,19 @@ class DeviceFamilyTests: XCTestCase { let deviceFamily = DeviceFamily(rawValue: "iPad") XCTAssert(deviceFamily == .iPad, "DeviceFamily - .iPad is failing") } - - func testDeviceFamilySimulator() { - let deviceFamily = DeviceFamily(rawValue: "x86_64") - XCTAssert(deviceFamily == .simulator, "DeviceFamily - .simulator is failing") + + func testInvalidDeviceFamily() { + let deviceFamily = DeviceFamily(rawValue: "Apple II") + XCTAssert(deviceFamily == .unknown, "DeviceFamily - .unknown is failing") + } + + func testDeviceIsSimulator() { + let deviceFamily = DeviceFamily(rawValue: "iPhone") + #if arch(i386) || arch(x86_64) + XCTAssert(deviceFamily.isSimulator, "DeviceFamily - .isSimulator is failing") + #else + XCTAssert(!(deviceFamily.isSimulator), "DeviceFamily - .isSimulator is failing") + #endif } + } diff --git a/Tests/DeviceModelTests.swift b/Tests/DeviceModelTests.swift index 05729f8..0abe55d 100644 --- a/Tests/DeviceModelTests.swift +++ b/Tests/DeviceModelTests.swift @@ -325,14 +325,13 @@ class DeviceModelTests: XCTestCase { XCTAssert(deviceModel == .iPodTouchSeventhGen , "DeviceModel - .iPodSeventhGen is failing") } - // MARK: Simulator Test + // MARK: Unknown Device Test - func testDeviceModelSimulator() { - let deviceModel = DeviceModel(identifier: Identifier("unknown")) - - XCTAssert(deviceModel == .unknown , "DeviceModel - .unknown is failing") + func testInvalidDeviceModel() { + let deviceModel = DeviceModel(identifier: Identifier("iPhone0,1")) + XCTAssert(deviceModel == .unknown, "DeviceModel - .unknown is failing") } - + // MARK: Notch test func testHasNotch() { let notchModels: [DeviceModel] = [.iPhoneX, .iPhoneXS, .iPhoneXSMax, .iPhoneXR, .iPhone11, .iPhone11Pro, .iPhone11ProMax] diff --git a/Tests/IdentifierTests.swift b/Tests/IdentifierTests.swift index 34d08ac..f70644c 100644 --- a/Tests/IdentifierTests.swift +++ b/Tests/IdentifierTests.swift @@ -62,30 +62,6 @@ class IdentifierTests: XCTestCase { "Identifier initilization is failing" ) } - - func testIdentifierInitSimulatorx86() { - let testString = "x86_64" - let identifier = Identifier(testString) - - XCTAssert( - (identifier.type == .simulator) && - (identifier.version.major == nil) && - (identifier.version.minor == nil), - "Identifier initilization is failing" - ) - } - - func testIdentifierInitSimulatori386() { - let testString = "i386" - let identifier = Identifier(testString) - - XCTAssert( - (identifier.type == .simulator) && - (identifier.version.major == nil) && - (identifier.version.minor == nil), - "Identifier initilization is failing" - ) - } // MARK: - iPhone String Description tests diff --git a/Tests/UIDeviceExtensionsTests.swift b/Tests/UIDeviceExtensionsTests.swift index 0c87d98..f53d6b9 100644 --- a/Tests/UIDeviceExtensionsTests.swift +++ b/Tests/UIDeviceExtensionsTests.swift @@ -51,8 +51,4 @@ class UIDeviceExtensionsTests: XCTestCase { XCTAssert(deviceFamily == .iPod, "DeviceExtensions - .isIpod is failing") } - func testDeviceExtensionsIsSimulator() { - let deviceFamily = DeviceFamily(rawValue: "x86_64") - XCTAssert(deviceFamily == .simulator, "DeviceExtensions - .isSimulator is failing") - } }