segmentio / analytics-ios

The hassle-free way to integrate analytics into any iOS application.
https://segment.com/libraries/ios
MIT License
402 stars 334 forks source link

Automatically collected lifecycle events not SwiftUI friendly #1035

Open loganblevins opened 2 years ago

loganblevins commented 2 years ago

Platform:

iOS + tvOS

Problem:

Segment auto tracked lifecycles events do not function with the SwiftUI lifecycle. Segment initialization requires UIApplication be valid to set up notification observers of global events such as: UIApplicationWillEnterForegroundNotification.

UIApplication instance is not available on init of SwiftUI lifecycle @main. To workaround this, the app delegate adaptor for UIKit lifecycle hooks must be implemented and Segment must be initialized in applicationDidFinishLaunching.

This is not documented by Segment and can easily go unnoticed until it is realized that the auto tracked lifecycle events are not being fired.

My suggestion is that Segment not require UIApplication to be valid for setting up notification observers of global events for auto tracked lifecycle events.

arvindkanjariya commented 1 year ago

Hi @loganblevins I have app with SwiftUI facing same issue trackApplicationLifecycleEvents is not working in app

any suggestion how to fix that

arvindkanjariya commented 1 year ago

Platform:

iOS : SwiftUI

I configured everything as per SDK documents TrackApplicationLifecycleEvents = true as well

But Application Opened, Application Installed, Application Updated is not functional in app

Any special things need to configure for this for SwiftUI to work on this event.

grennis commented 1 year ago

I would also comment that this is more than just an minor documentation issue, since it is very likely that the app will be doing things during App initialization (like identifying the user) that requires that Segment has already been initialized. So if we have to wait for AppDelete didFinishLaunching to initialize Segment, then those attempts to do anything will crash. We ended up just doing our own lifecycle tracking and turned off Segment's for now.