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

Ensure not to make SwiftUI previews crash due to missing init call #74

Closed Jeehut closed 1 year ago

Jeehut commented 1 year ago

I found and used this method for checking if we're running for SwiftUI previews here.

I made sure to order the if checks so that the performance when running in a real world application shouldn't get affected. Also, I turned off sending signals when in SwiftUI preview mode as I don't think this is real user-initiated app usage when Xcode automatically re-renders and calls .onAppear every few seconds during a coding session.

Note that I followed the exact same implementation as for testMode to ensure users can always override the behavior by setting a different value for swiftUIPreviewMode, e.g. in case they actually run the application from SwiftUI for whatever reason instead of using the Run button in Xcode.

I'm not sure if swiftUIPreviewMode is the best name though. It describes the default behavior very well, but I can imagine someone might use this same setting and set it to true for turning off analytics when a user disables such an option in an apps settings (opt-out), therefore maybe the property could be renamed to something like sendSignals. But that's a change separate from fixing just the SwiftUI issue, so I was not sure if you want to introduce such an option.

Fixes https://github.com/TelemetryDeck/SwiftClient/issues/73.

winsmith commented 1 year ago

Lovely! Thank you very much!