Skip to content

Fixed applicationDidFinishLaunching issue when using SwiftUI / Scenes #126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Sources/Segment/Plugins/Platforms/iOS/iOSLifecycleEvents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,18 @@ class iOSLifecycleEvents: PlatformPlugin, iOSLifecycle {
let type = PluginType.before
var analytics: Analytics?

/// Since application:didFinishLaunchingWithOptions is not automatically called with Scenes / SwiftUI,
/// this gets around by using a flag in user defaults to check for big events like application updating,
/// being installed or even opening.
@Atomic
private var didFinishLaunching = false

func application(_ application: UIApplication?, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) {

// Make sure we aren't double calling application:didFinishLaunchingWithOptions
// by resetting the check at the start
didFinishLaunching = true

if analytics?.configuration.values.trackApplicationLifecycleEvents == false {
return
}
Expand Down Expand Up @@ -77,6 +88,19 @@ class iOSLifecycleEvents: PlatformPlugin, iOSLifecycle {

analytics?.track(name: "Application Backgrounded")
}

func applicationDidBecomeActive(application: UIApplication?) {
if analytics?.configuration.values.trackApplicationLifecycleEvents == false {
return
}

// Lets check if we skipped application:didFinishLaunchingWithOptions,
// if so, lets call it.
if didFinishLaunching == false {
// Call application did finish launching
self.application(nil, didFinishLaunchingWithOptions: nil)
}
}
}

#endif