analytics-ios icon indicating copy to clipboard operation
analytics-ios copied to clipboard

Automatically collected lifecycle events not SwiftUI friendly

Open loganblevins opened this issue 2 years ago • 3 comments

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.

loganblevins avatar May 26 '22 19:05 loganblevins

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

any suggestion how to fix that

arvindkanjariya avatar Feb 15 '23 13:02 arvindkanjariya

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.

arvindkanjariya avatar Feb 15 '23 14:02 arvindkanjariya

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.

grennis avatar May 31 '23 13:05 grennis