Skip to content
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

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

Merged
merged 6 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
Binary file added Images/TARGET_APP_EXTENSION.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 25 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,29 @@ 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>

## App Extensions Support

When using this SDK in an app extension target, add `TARGET_APP_EXTENSION` to your build settings to ensure extension-safe API usage:

1. In Xcode, select your app extension target
1. Go to "Build Settings"
1. Find "Active Compilation Conditions" (make sure you have the "All" tab selected)
1. Add `TARGET_APP_EXTENSION` to the Debug and Release configurations

![App Extension Build Settings](Images/TARGET_APP_EXTENSION.jpeg)

> [!TIP]
> You can can also just **copy** the following two lines, select the build setting and **paste** them in:
> ```
> SWIFT_ACTIVE_COMPILATION_CONDITIONS[config=Debug] = TARGET_APP_EXTENSION DEBUG
> SWIFT_ACTIVE_COMPILATION_CONDITIONS[config=Release] = TARGET_APP_EXTENSION
> ```

> [!NOTE]
> Only add this compilation condition to **extension** targets, not to your main app target.

## Sessions

Expand Down
32 changes: 18 additions & 14 deletions Sources/TelemetryDeck/Signals/Signal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,21 +123,25 @@ 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)"
}

#if !TARGET_APP_EXTENSION
a11yParams["TelemetryDeck.Accessibility.preferredContentSizeCategory"] = UIApplication.shared.preferredContentSizeCategory.rawValue
.replacingOccurrences(of: "UICTContentSizeCategory", with: "") // replaces output "UICTContentSizeCategoryL" with "L"
#endif

#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
4 changes: 2 additions & 2 deletions Sources/TelemetryDeck/Signals/SignalManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ private extension SignalManager {
@objc func appWillTerminate() {
configuration.logHandler?.log(.debug, message: #function)

#if os(watchOS) || os(macOS)
#if os(watchOS) || os(macOS) || TARGET_APP_EXTENSION
self.signalCache.backupCache()
#else
// run backup in background task to avoid blocking main thread while ensuring app stays open during write
Expand Down Expand Up @@ -192,7 +192,7 @@ private extension SignalManager {
sendTimer?.invalidate()
sendTimer = nil

#if os(watchOS) || os(macOS)
#if os(watchOS) || os(macOS) || TARGET_APP_EXTENSION
self.signalCache.backupCache()
#else
// run backup in background task to avoid blocking main thread while ensuring app stays open during write
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