Skip to content

Commit 02f791e

Browse files
authored
Merge pull request #22 from manucheri/20240429-fix-devicemodel-mac
Fix device model identifier for Mac
2 parents 62e47da + 739bca7 commit 02f791e

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

Sources/Aptabase/EnvironmentInfo.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,22 @@ struct EnvironmentInfo {
7575
}
7676

7777
private static var deviceModel: String {
78+
#if os(macOS) || targetEnvironment(macCatalyst)
79+
// `uname` returns x86_64 (or Apple Silicon equivalent) for Macs.
80+
// Use `sysctlbyname` here instead to get actual model name. If it fails, fall back to `uname`.
81+
var size = 0
82+
sysctlbyname("hw.model", nil, &size, nil, 0)
83+
if size > 0 {
84+
var model = [CChar](repeating: 0, count: size)
85+
sysctlbyname("hw.model", &model, &size, nil, 0)
86+
let deviceModel = String(cString: model)
87+
// If we got a deviceModel, use it. Else continue to "default" logic.
88+
if !deviceModel.isEmpty {
89+
return deviceModel
90+
}
91+
}
92+
#endif
93+
7894
if let simulatorModelIdentifier = ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"] {
7995
return simulatorModelIdentifier
8096
} else {

0 commit comments

Comments
 (0)