Skip to content

Commit

Permalink
Fix device model for Mac
Browse files Browse the repository at this point in the history
`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).
  • Loading branch information
manucheri committed Apr 29, 2024
1 parent 62e47da commit 739bca7
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Sources/Aptabase/EnvironmentInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,22 @@ struct EnvironmentInfo {
}

private static var deviceModel: String {
#if os(macOS) || targetEnvironment(macCatalyst)
// `uname` returns x86_64 (or Apple Silicon equivalent) for Macs.
// Use `sysctlbyname` here instead to get actual model name. If it fails, fall back to `uname`.
var size = 0
sysctlbyname("hw.model", nil, &size, nil, 0)
if size > 0 {
var model = [CChar](repeating: 0, count: size)
sysctlbyname("hw.model", &model, &size, nil, 0)
let deviceModel = String(cString: model)
// If we got a deviceModel, use it. Else continue to "default" logic.
if !deviceModel.isEmpty {
return deviceModel
}
}
#endif

if let simulatorModelIdentifier = ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"] {
return simulatorModelIdentifier
} else {
Expand Down

0 comments on commit 739bca7

Please sign in to comment.