From 739bca7ee457e4931226715a48b62e0395734dd4 Mon Sep 17 00:00:00 2001 From: Aron Manucheri Date: Mon, 29 Apr 2024 18:42:02 +0200 Subject: [PATCH] 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). --- Sources/Aptabase/EnvironmentInfo.swift | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Sources/Aptabase/EnvironmentInfo.swift b/Sources/Aptabase/EnvironmentInfo.swift index 0c854ef..c90fbb7 100644 --- a/Sources/Aptabase/EnvironmentInfo.swift +++ b/Sources/Aptabase/EnvironmentInfo.swift @@ -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 {