Skip to content

Commit

Permalink
Merge branch 'main' into feat/support-host-with-path
Browse files Browse the repository at this point in the history
  • Loading branch information
ioannisj authored Feb 4, 2025
2 parents 713a525 + fb53d3b commit 154bf79
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 27 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Next

- fix: custom hosts with a path ([#290](https://github.com/PostHog/posthog-ios/pull/290))
- fix: identify macOS when running Mac Catalyst or iOS on Mac ([#287](https://github.com/PostHog/posthog-ios/pull/287))

## 3.19.2 - 2025-01-30

Expand Down
6 changes: 3 additions & 3 deletions PostHog.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -2025,11 +2025,11 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited)";
MACOSX_DEPLOYMENT_TARGET = 10.15;
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = com.posthog.PostHogExample.yj;
PRODUCT_BUNDLE_IDENTIFIER = com.posthog.PostHogExample;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SUPPORTED_PLATFORMS = "iphoneos iphonesimulator";
SUPPORTS_MACCATALYST = NO;
SUPPORTS_MACCATALYST = YES;
SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = NO;
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_VERSION = 5.0;
Expand Down Expand Up @@ -2064,7 +2064,7 @@
PRODUCT_BUNDLE_IDENTIFIER = com.posthog.PostHogExample;
PRODUCT_NAME = "$(TARGET_NAME)";
SUPPORTED_PLATFORMS = "iphoneos iphonesimulator";
SUPPORTS_MACCATALYST = NO;
SUPPORTS_MACCATALYST = YES;
SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = NO;
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_VERSION = 5.0;
Expand Down
110 changes: 86 additions & 24 deletions PostHog/PostHogContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
} 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"
Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit 154bf79

Please sign in to comment.