Skip to content

Avoid potential compilation errors in app extensions when using CocoaPods #229

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

Merged
merged 6 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,11 @@ You can also send additional parameters with each signal:
TelemetryDeck.signal("Database.updated", parameters: ["numberOfDatabaseEntries": "3831"])
```

TelemetryDeck will automatically send base parameters, such as:
<details>
<summary>TelemetryDeck will automatically send base parameters, expand to see common examples</summary>

- TelemetryDeck.Accessibility.isBoldTextEnabled
- TelemetryDeck.Accessibility.isDarkerSystemColorsEnabled
- TelemetryDeck.Accessibility.isInvertColorsEnabled
- TelemetryDeck.Accessibility.isReduceMotionEnabled
- TelemetryDeck.Accessibility.isReduceTransparencyEnabled
- TelemetryDeck.Accessibility.isSwitchControlEnabled
- TelemetryDeck.Accessibility.isVoiceOverEnabled
- TelemetryDeck.Accessibility.preferredContentSizeCategory
- TelemetryDeck.Accessibility.shouldDifferentiateWithoutColor

- TelemetryDeck.AppInfo.buildNumber
- TelemetryDeck.AppInfo.version
- TelemetryDeck.Device.architecture
Expand All @@ -109,7 +102,8 @@ TelemetryDeck will automatically send base parameters, such as:
- TelemetryDeck.UserPreference.layoutDirection
- TelemetryDeck.UserPreference.region

See our [Grand Renaming article](https://telemetrydeck.com/docs/articles/grand-rename/?source=github) for a full list.
See our [related documentation page](https://telemetrydeck.com/docs/api/default-parameters/?source=github.com) for a full list.
</details>

## Sessions

Expand Down
67 changes: 34 additions & 33 deletions Sources/TelemetryDeck/Signals/Signal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,21 +123,26 @@ extension DefaultSignalPayload {
var a11yParams: [String: String] = [:]

#if os(iOS) || os(tvOS)
a11yParams["TelemetryDeck.Accessibility.isReduceMotionEnabled"] = "\(UIAccessibility.isReduceMotionEnabled)"
a11yParams["TelemetryDeck.Accessibility.isBoldTextEnabled"] = "\(UIAccessibility.isBoldTextEnabled)"
a11yParams["TelemetryDeck.Accessibility.isInvertColorsEnabled"] = "\(UIAccessibility.isInvertColorsEnabled)"
a11yParams["TelemetryDeck.Accessibility.isDarkerSystemColorsEnabled"] = "\(UIAccessibility.isDarkerSystemColorsEnabled)"
a11yParams["TelemetryDeck.Accessibility.isReduceTransparencyEnabled"] = "\(UIAccessibility.isReduceTransparencyEnabled)"
if #available(iOS 13.0, *) {
a11yParams["TelemetryDeck.Accessibility.shouldDifferentiateWithoutColor"] = "\(UIAccessibility.shouldDifferentiateWithoutColor)"
}
a11yParams["TelemetryDeck.Accessibility.preferredContentSizeCategory"] = UIApplication.shared.preferredContentSizeCategory.rawValue
.replacingOccurrences(of: "UICTContentSizeCategory", with: "") // replaces output "UICTContentSizeCategoryL" with "L"
a11yParams["TelemetryDeck.Accessibility.isReduceMotionEnabled"] = "\(UIAccessibility.isReduceMotionEnabled)"
a11yParams["TelemetryDeck.Accessibility.isBoldTextEnabled"] = "\(UIAccessibility.isBoldTextEnabled)"
a11yParams["TelemetryDeck.Accessibility.isInvertColorsEnabled"] = "\(UIAccessibility.isInvertColorsEnabled)"
a11yParams["TelemetryDeck.Accessibility.isDarkerSystemColorsEnabled"] = "\(UIAccessibility.isDarkerSystemColorsEnabled)"
a11yParams["TelemetryDeck.Accessibility.isReduceTransparencyEnabled"] = "\(UIAccessibility.isReduceTransparencyEnabled)"
if #available(iOS 13.0, *) {
a11yParams["TelemetryDeck.Accessibility.shouldDifferentiateWithoutColor"] = "\(UIAccessibility.shouldDifferentiateWithoutColor)"
}

// in app extensions `UIApplication.shared` is not available
if !Bundle.main.bundlePath.hasSuffix(".appex") {
a11yParams["TelemetryDeck.Accessibility.preferredContentSizeCategory"] = UIApplication.shared.preferredContentSizeCategory.rawValue
.replacingOccurrences(of: "UICTContentSizeCategory", with: "") // replaces output "UICTContentSizeCategoryL" with "L"
}

#elseif os(macOS)
if let systemPrefs = UserDefaults.standard.persistentDomain(forName: "com.apple.universalaccess") {
a11yParams["TelemetryDeck.Accessibility.isReduceMotionEnabled"] = "\(systemPrefs["reduceMotion"] as? Bool ?? false)"
a11yParams["TelemetryDeck.Accessibility.isInvertColorsEnabled"] = "\(systemPrefs["InvertColors"] as? Bool ?? false)"
}
if let systemPrefs = UserDefaults.standard.persistentDomain(forName: "com.apple.universalaccess") {
a11yParams["TelemetryDeck.Accessibility.isReduceMotionEnabled"] = "\(systemPrefs["reduceMotion"] as? Bool ?? false)"
a11yParams["TelemetryDeck.Accessibility.isInvertColorsEnabled"] = "\(systemPrefs["InvertColors"] as? Bool ?? false)"
}
#endif

return a11yParams
Expand Down Expand Up @@ -373,22 +378,16 @@ extension DefaultSignalPayload {
static var colorScheme: String {
#if os(iOS) || os(tvOS)
switch UIScreen.main.traitCollection.userInterfaceStyle {
case .dark:
return "Dark"
case .light:
return "Light"
default:
return "N/A"
case .dark: return "Dark"
case .light: return "Light"
default: return "N/A"
}
#elseif os(macOS)
if #available(macOS 10.14, *) {
switch NSAppearance.current.name {
case .aqua:
return "Light"
case .darkAqua:
return "Dark"
default:
return "N/A"
case .aqua: return "Light"
case .darkAqua: return "Dark"
default: return "N/A"
}
} else {
return "Light"
Expand All @@ -402,7 +401,12 @@ extension DefaultSignalPayload {
@MainActor
static var layoutDirection: String {
#if os(iOS) || os(tvOS)
return UIApplication.shared.userInterfaceLayoutDirection == .leftToRight ? "leftToRight" : "rightToLeft"
if Bundle.main.bundlePath.hasSuffix(".appex") {
// we're in an app extension, where `UIApplication.shared` is not available
return "N/A"
} else {
return UIApplication.shared.userInterfaceLayoutDirection == .leftToRight ? "leftToRight" : "rightToLeft"
}
#elseif os(macOS)
if let nsApp = NSApp {
return nsApp.userInterfaceLayoutDirection == .leftToRight ? "leftToRight" : "rightToLeft"
Expand Down Expand Up @@ -467,12 +471,9 @@ extension DefaultSignalPayload {
static var orientation: String {
#if os(iOS)
switch UIDevice.current.orientation {
case .portrait, .portraitUpsideDown:
return "Portrait"
case .landscapeLeft, .landscapeRight:
return "Landscape"
default:
return "Unknown"
case .portrait, .portraitUpsideDown: return "Portrait"
case .landscapeLeft, .landscapeRight: return "Landscape"
default: return "Unknown"
}
#else
return "Fixed"
Expand Down
30 changes: 20 additions & 10 deletions Sources/TelemetryDeck/Signals/SignalManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,18 @@ private extension SignalManager {
#if os(watchOS) || os(macOS)
self.signalCache.backupCache()
#else
// run backup in background task to avoid blocking main thread while ensuring app stays open during write
let backgroundTaskID = UIApplication.shared.beginBackgroundTask()
DispatchQueue.global(qos: .background).async {
if Bundle.main.bundlePath.hasSuffix(".appex") {
// we're in an app extension, where `UIApplication.shared` is not available
self.signalCache.backupCache()
} else {
// run backup in background task to avoid blocking main thread while ensuring app stays open during write
let backgroundTaskID = UIApplication.shared.beginBackgroundTask()
DispatchQueue.global(qos: .background).async {
self.signalCache.backupCache()

DispatchQueue.main.async {
UIApplication.shared.endBackgroundTask(backgroundTaskID)
DispatchQueue.main.async {
UIApplication.shared.endBackgroundTask(backgroundTaskID)
}
}
}
#endif
Expand Down Expand Up @@ -195,13 +200,18 @@ private extension SignalManager {
#if os(watchOS) || os(macOS)
self.signalCache.backupCache()
#else
// run backup in background task to avoid blocking main thread while ensuring app stays open during write
let backgroundTaskID = UIApplication.shared.beginBackgroundTask()
DispatchQueue.global(qos: .background).async {
if Bundle.main.bundlePath.hasSuffix(".appex") {
// we're in an app extension, where `UIApplication.shared` is not available
self.signalCache.backupCache()
} else {
// run backup in background task to avoid blocking main thread while ensuring app stays open during write
let backgroundTaskID = UIApplication.shared.beginBackgroundTask()
DispatchQueue.global(qos: .background).async {
self.signalCache.backupCache()

DispatchQueue.main.async {
UIApplication.shared.endBackgroundTask(backgroundTaskID)
DispatchQueue.main.async {
UIApplication.shared.endBackgroundTask(backgroundTaskID)
}
}
}
#endif
Expand Down
5 changes: 3 additions & 2 deletions Sources/TelemetryDeck/TelemetryDeck.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,10 @@ public enum TelemetryDeck {
@MainActor
@available(watchOS 7.0, *)
public static func stopAndSendDurationSignal(_ signalName: String, parameters: [String: String] = [:]) {
guard let (duration, startParameters) = DurationSignalTracker.shared.stopTracking(signalName) else { return }
guard let (exactDuration, startParameters) = DurationSignalTracker.shared.stopTracking(signalName) else { return }
let roundedDuration = (exactDuration * 1_000).rounded(.down) * 1_000 // rounds down to 3 fraction digits

var durationParameters = ["TelemetryDeck.Signal.durationInSeconds": String(duration)]
var durationParameters = ["TelemetryDeck.Signal.durationInSeconds": String(roundedDuration)]
durationParameters.merge(startParameters) { $1 }

self.internalSignal(signalName, parameters: durationParameters.merging(parameters) { $1 })
Expand Down