如何在新的SwiftUIApp生命周期中处理SceneDelegate willConnectTo方法

问题描述

如何在新的SwiftUI应用生命周期中处理此方法? 我正在查看场景,但没有有关SceneDelegate方法的任何信息

func scene(_ scene: UIScene,willConnectTo session: UIScenesession,options connectionoptions: UIScene.Connectionoptions) { method in SwiftUI new App cycle.

解决方法

使用.onReceive修饰符可以在设备收到特定通知时收到通知。 对于您的情况,您可以在其中一个视图的顶部使用.onReceive这样的通知来通知场景连接:

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

即使应用未运行且已从通知中启动,您也可以通过userNotificationCenter接收通知。

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