How to listen for didFinishLaunchingNotification in SwiftUI?

Hi all,

I'm trying to detect an app launch in a multiplatform SwiftUI app (iOS 14/macOS 11) and having a heck of a time getting onReceive() handlers to catch didFinishLaunchingNotification, despite other notifications are being received as expected.

Here's a simplified version of what I'm trying:

Code Block swift
import SwiftUI
struct ContentView: View {
#if os(iOS)
let didFinishLaunchingNotification = UIApplication.didFinishLaunchingNotification
let didBecomeActiveNotification = UIApplication.didBecomeActiveNotification
#else
let didFinishLaunchingNotification = NSApplication.didFinishLaunchingNotification
let didBecomeActiveNotification = NSApplication.didBecomeActiveNotification
#endif
var body: some View {
NavigationView {
SidebarView()
ListView()
Text("Content!")
}
.onReceive(NotificationCenter.default.publisher(for: didFinishLaunchingNotification)) { _ in
print("didFinishLaunchingNotification fired")
}
.onReceive(NotificationCenter.default.publisher(for: didBecomeActiveNotification)) { _ in
print("didBecomeActiveNotification fired")
}
}
}


Do you all see anything wrong with this approach? Is this a bug in SwiftUI?

Thanks in advance!
Can't update the original post, but I should mention that this works fine in the macOS 11 target; it's the iOS 14 target for the app that's not getting the notification.
How to listen for didFinishLaunchingNotification in SwiftUI?
 
 
Q