TelemetryDeck / SwiftSDK

Swift SDK for TelemetryDeck, a privacy-conscious analytics service for apps and websites.
https://telemetrydeck.com/
Other
149 stars 30 forks source link

Event delivery from app extensions #104

Closed palmin closed 1 year ago

palmin commented 1 year ago

My app is extension centric with much of the work it performs happening outside the main app in file-provider and share extensions.

I can't seem to get events to be delivered from extensions even through I do the same TelemetryManager initialization when extensions launch as I do in applicationDidLaunch for the main app.

Looking superficially through the source code it looks like it could be problematic that extensions never enter or leave foreground mode.

Is there anything special I can do to get event delivery working from extensions?

palmin commented 1 year ago

In case anyone else runs into similar problems I had to do two things to solve this.

I had to take care to call TelemetryManager.initialize on the main thread as the thread that launches a file provider has no run-loop breaking the timer that sends out events.

To make sure unsent events are cached when extension closes I had to manually send out notifications in NSFileProviderExtension.invalidate() making it look like the app was closing.

#if os(macOS)
        NotificationCenter.default.post(name: NSApplication.willTerminateNotification, object: nil)
#elseif os(iOS)
        NotificationCenter.default.post(name: UIApplication.didEnterBackgroundNotification, object: nil)
#endif
winsmith commented 1 year ago

Fantastic, thank you!