Skip to content

Commit 739bca7

Browse files
committed
Fix device model for Mac
`uname` returns x86_64 (or Apple Silicon equivalent) for Macs. Get the device name in another way for Mac devices. If it fails, fallback to the default way (can be represented as "unknown Mac" or similar).
1 parent 62e47da commit 739bca7

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)