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

Supports obtaining the identifier of the corresponding device under the simulator #10

Merged
merged 1 commit into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ All notable changes to this project are documented in this file.

## [Unreleased](https://github.com/RakuyoKit/RakuyoKit/compare/1.0.4...HEAD)

### Changed

- `Device.codeName` supports obtaining the real device identification corresponding to the current simulator. [#10](https://github.com/RakuyoKit/RakuyoKit/pull/10) @rakuyoMo

-----

## [1.0.4 - Format and Lint](https://github.com/RakuyoKit/RakuyoKit/releases/tag/1.0.4) (2024-4-21)
Expand Down
57 changes: 52 additions & 5 deletions Sources/Core/Utilities/Device.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,70 @@

import UIKit

// MARK: - Device

public enum Device {
public static func generateUUID() -> String {
UUID().uuidString.replacingOccurrences(of: "-", with: "").lowercased()
}
}

// MARK: - Code Name

extension Device {
public enum Running {
case realMachine
case simulator
case wherever
}

/// Device model code
///
/// like iPhone1,1
public static var codeName: String {
codeName(running: .wherever)
}

/// When running on a real machine in the physical world,
/// the identifier corresponding to the device is returned.
///
/// like `iPhone1,1`
private static var realMachineCodeName: String {
var info = utsname()
uname(&info)

let machineMirror = Mirror(reflecting: info.machine)

return machineMirror.children.reduce(into: "") {
guard let value = $1.value as? Int8, value != 0 else { return }
$0 += String(UnicodeScalar(UInt8(value)))
}
}

public static func generateUUID() -> String {
UUID().uuidString.replacingOccurrences(of: "-", with: "").lowercased()

/// When the simulator is running, the identifier of
/// the real device corresponding to the simulator is returned.
private static var simulatorCodeName: String? {
ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"]
}

/// Device model code
///
/// like iPhone1,1
public static func codeName(running: Running) -> String {
switch running {
case .realMachine:
return realMachineCodeName

case .simulator:
if let _codeName = simulatorCodeName { return _codeName }
assertionFailure(
"Please call this property when the simulator is running." +
" Consider using `#if targetEnvironment(simulator)` to isolate your code."
)
return realMachineCodeName

case .wherever:
return simulatorCodeName ?? realMachineCodeName
}
}
}