Skip to content

Commit 3b9cf34

Browse files
committed
Check for app extension at runtime & avoid calling UIApplication.shared
1 parent 7501510 commit 3b9cf34

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

Sources/TelemetryDeck/Signals/Signal.swift

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,11 @@ extension DefaultSignalPayload {
132132
a11yParams["TelemetryDeck.Accessibility.shouldDifferentiateWithoutColor"] = "\(UIAccessibility.shouldDifferentiateWithoutColor)"
133133
}
134134

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

138141
#elseif os(macOS)
139142
if let systemPrefs = UserDefaults.standard.persistentDomain(forName: "com.apple.universalaccess") {
@@ -404,7 +407,12 @@ extension DefaultSignalPayload {
404407
@MainActor
405408
static var layoutDirection: String {
406409
#if os(iOS) || os(tvOS)
407-
return UIApplication.shared.userInterfaceLayoutDirection == .leftToRight ? "leftToRight" : "rightToLeft"
410+
if Bundle.main.bundlePath.hasSuffix(".appex") {
411+
// we're in an app extension, where `UIApplication.shared` is not available
412+
return "N/A"
413+
} else {
414+
return UIApplication.shared.userInterfaceLayoutDirection == .leftToRight ? "leftToRight" : "rightToLeft"
415+
}
408416
#elseif os(macOS)
409417
if let nsApp = NSApp {
410418
return nsApp.userInterfaceLayoutDirection == .leftToRight ? "leftToRight" : "rightToLeft"

Sources/TelemetryDeck/Signals/SignalManager.swift

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,18 @@ private extension SignalManager {
157157
#if os(watchOS) || os(macOS)
158158
self.signalCache.backupCache()
159159
#else
160-
// run backup in background task to avoid blocking main thread while ensuring app stays open during write
161-
let backgroundTaskID = UIApplication.shared.beginBackgroundTask()
162-
DispatchQueue.global(qos: .background).async {
160+
if Bundle.main.bundlePath.hasSuffix(".appex") {
161+
// we're in an app extension, where `UIApplication.shared` is not available
163162
self.signalCache.backupCache()
163+
} else {
164+
// run backup in background task to avoid blocking main thread while ensuring app stays open during write
165+
let backgroundTaskID = UIApplication.shared.beginBackgroundTask()
166+
DispatchQueue.global(qos: .background).async {
167+
self.signalCache.backupCache()
164168

165-
DispatchQueue.main.async {
166-
UIApplication.shared.endBackgroundTask(backgroundTaskID)
169+
DispatchQueue.main.async {
170+
UIApplication.shared.endBackgroundTask(backgroundTaskID)
171+
}
167172
}
168173
}
169174
#endif
@@ -195,13 +200,18 @@ private extension SignalManager {
195200
#if os(watchOS) || os(macOS)
196201
self.signalCache.backupCache()
197202
#else
198-
// run backup in background task to avoid blocking main thread while ensuring app stays open during write
199-
let backgroundTaskID = UIApplication.shared.beginBackgroundTask()
200-
DispatchQueue.global(qos: .background).async {
203+
if Bundle.main.bundlePath.hasSuffix(".appex") {
204+
// we're in an app extension, where `UIApplication.shared` is not available
201205
self.signalCache.backupCache()
206+
} else {
207+
// run backup in background task to avoid blocking main thread while ensuring app stays open during write
208+
let backgroundTaskID = UIApplication.shared.beginBackgroundTask()
209+
DispatchQueue.global(qos: .background).async {
210+
self.signalCache.backupCache()
202211

203-
DispatchQueue.main.async {
204-
UIApplication.shared.endBackgroundTask(backgroundTaskID)
212+
DispatchQueue.main.async {
213+
UIApplication.shared.endBackgroundTask(backgroundTaskID)
214+
}
205215
}
206216
}
207217
#endif

0 commit comments

Comments
 (0)