karmjit singh
karmjit singh

Reputation: 279

How to handle SceneDelegate willConnectTo method in new SwiftUIApp lifecycle

How do I handle this method in new SwiftUI app lifecycle? I am looking at the Scene but it does not have any info about SceneDelegate methods

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { method in SwiftUI new App cycle.

Upvotes: 1

Views: 1653

Answers (2)

user12208004
user12208004

Reputation: 1998

You can get notification through userNotificationCenter even when the app is not running and is launched from the notification.

func userNotificationCenter(_ center: UNUserNotificationCenter,
                                didReceive response: UNNotificationResponse,
                                withCompletionHandler completionHandler: @escaping () -> Void) {
        print("open notification")
        let userInfo = response.notification.request.content.userInfo
        
        print(userInfo)
        completionHandler()
    }

Upvotes: 0

Mahdi BM
Mahdi BM

Reputation: 2104

You can get notified when a device receives a specific notification using .onReceive modifier. for your case, you can use .onReceive like this on top of one of your view(s) to get notified for scene connections:

.onReceive(NotificationCenter.default.publisher(for: UIScene.willConnectNotification)) { notification in
                
            }

Upvotes: 3

Related Questions