-
Notifications
You must be signed in to change notification settings - Fork 61
fix: identify mac catalyst #287
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
Changes from all commits
2935d13
6951441
9e61ce1
82afb10
2a5e88e
b7c867d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,38 +53,85 @@ class PostHogContext { | |
properties["$is_emulator"] = false | ||
#endif | ||
|
||
// iOS app running in compatibility mode (Designed for iPad/iPhone) | ||
var isiOSAppOnMac = false | ||
#if os(iOS) | ||
if #available(iOS 14.0, *) { | ||
isiOSAppOnMac = ProcessInfo.processInfo.isiOSAppOnMac | ||
} | ||
#endif | ||
|
||
// iPad app running on Mac Catalyst | ||
#if targetEnvironment(macCatalyst) | ||
let isMacCatalystApp = true | ||
#else | ||
let isMacCatalystApp = false | ||
#endif | ||
|
||
properties["$is_ios_running_on_mac"] = isiOSAppOnMac | ||
properties["$is_mac_catalyst_app"] = isMacCatalystApp | ||
|
||
#if os(iOS) || os(tvOS) | ||
let device = UIDevice.current | ||
// use https://github.com/devicekit/DeviceKit | ||
properties["$device_name"] = device.model | ||
properties["$os_name"] = device.systemName | ||
properties["$os_version"] = device.systemVersion | ||
|
||
var deviceType: String? | ||
switch device.userInterfaceIdiom { | ||
case UIUserInterfaceIdiom.phone: | ||
deviceType = "Mobile" | ||
case UIUserInterfaceIdiom.pad: | ||
deviceType = "Tablet" | ||
case UIUserInterfaceIdiom.tv: | ||
deviceType = "TV" | ||
case UIUserInterfaceIdiom.carPlay: | ||
deviceType = "CarPlay" | ||
case UIUserInterfaceIdiom.mac: | ||
deviceType = "Desktop" | ||
default: | ||
deviceType = nil | ||
} | ||
if deviceType != nil { | ||
properties["$device_type"] = deviceType | ||
let processInfo = ProcessInfo.processInfo | ||
|
||
if isMacCatalystApp || isiOSAppOnMac { | ||
let underlyingOS = device.systemName | ||
let underlyingOSVersion = device.systemVersion | ||
let macOSVersion = processInfo.operatingSystemVersionString | ||
|
||
if isMacCatalystApp { | ||
let osVersion = ProcessInfo.processInfo.operatingSystemVersion | ||
properties["$os_version"] = "\(osVersion.majorVersion).\(osVersion.minorVersion).\(osVersion.patchVersion)" | ||
} else { | ||
let osVersionString = processInfo.operatingSystemVersionString | ||
if let versionRange = osVersionString.range(of: #"\d+\.\d+\.\d+"#, options: .regularExpression) { | ||
properties["$os_version"] = osVersionString[versionRange] | ||
} else { | ||
// fallback to full version string in case formatting changes | ||
properties["$os_version"] = osVersionString | ||
} | ||
} | ||
// device.userInterfaceIdiom reports .pad here, so we use a static value instead | ||
// - For an app deployable on iPad, the idiom type is always .pad (instead of .mac) | ||
// | ||
// Source: https://developer.apple.com/documentation/apple-silicon/adapting-ios-code-to-run-in-the-macos-environment#Handle-unknown-device-types-gracefully | ||
properties["$os_name"] = "macOS" | ||
properties["$device_type"] = "Desktop" | ||
properties["$device_name"] = processInfo.hostName | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what does hostName return here? this is about the device name (Model), and not the user's name or device's nickname There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is equivalent to I think both return the computer name which could leak PII actually, which I agree is concerning. We describe Regarding the actual device model, we capture as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should be the info of the physical device. |
||
} else { | ||
// use https://github.com/devicekit/DeviceKit | ||
properties["$os_name"] = device.systemName | ||
properties["$os_version"] = device.systemVersion | ||
properties["$device_name"] = device.model | ||
|
||
var deviceType: String? | ||
switch device.userInterfaceIdiom { | ||
case UIUserInterfaceIdiom.phone: | ||
deviceType = "Mobile" | ||
case UIUserInterfaceIdiom.pad: | ||
deviceType = "Tablet" | ||
case UIUserInterfaceIdiom.tv: | ||
deviceType = "TV" | ||
case UIUserInterfaceIdiom.carPlay: | ||
deviceType = "CarPlay" | ||
case UIUserInterfaceIdiom.mac: | ||
deviceType = "Desktop" | ||
default: | ||
deviceType = nil | ||
} | ||
if deviceType != nil { | ||
properties["$device_type"] = deviceType | ||
} | ||
} | ||
#elseif os(macOS) | ||
let deviceName = Host.current().localizedName | ||
if (deviceName?.isEmpty) != nil { | ||
properties["$device_name"] = deviceName | ||
} | ||
let processInfo = ProcessInfo.processInfo | ||
properties["$os_name"] = "macOS \(processInfo.operatingSystemVersionString)" // eg Version 14.2.1 (Build 23C71) | ||
properties["$os_name"] = "macOS" | ||
let osVersion = processInfo.operatingSystemVersion | ||
properties["$os_version"] = "\(osVersion.majorVersion).\(osVersion.minorVersion).\(osVersion.patchVersion)" | ||
properties["$device_type"] = "Desktop" | ||
|
@@ -134,10 +181,25 @@ class PostHogContext { | |
} | ||
|
||
private func platform() -> String { | ||
var sysctlName = "hw.machine" | ||
|
||
// In case of mac catalyst or iOS running on mac: | ||
// - "hw.machine" returns underlying iPad/iPhone model | ||
// - "hw.model" returns mac model | ||
#if targetEnvironment(macCatalyst) | ||
sysctlName = "hw.model" | ||
#elseif os(iOS) | ||
if #available(iOS 14.0, *) { | ||
if ProcessInfo.processInfo.isiOSAppOnMac { | ||
sysctlName = "hw.model" | ||
} | ||
} | ||
#endif | ||
|
||
var size = 0 | ||
sysctlbyname("hw.machine", nil, &size, nil, 0) | ||
sysctlbyname(sysctlName, nil, &size, nil, 0) | ||
var machine = [CChar](repeating: 0, count: size) | ||
sysctlbyname("hw.machine", &machine, &size, nil, 0) | ||
sysctlbyname(sysctlName, &machine, &size, nil, 0) | ||
return String(cString: machine) | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.