IOS场景将进入前台处理通用链接

wa7juj8i  于 2023-03-24  发布在  iOS
关注(0)|答案(1)|浏览(146)

在我的iOS应用程序中,我在SceneDelegate类中有以下函数:

func sceneWillEnterForeground(_ scene: UIScene) {
    print("... sceneWillEnterForeground ALWAYS CALLED")
    NotificationCenter.default.post(name: Notification.Name("update"), object: nil) // ONLY TO BE CALLED when universal link is not clicked
}

func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
    print("SCENE WITH UNIVERSAL LINK CLICK")
    guard let url = userActivity.webpageURL else { return }
    let userInfo: [String: String] = ["url": url.absoluteString]
    NotificationCenter.default.post(name: NSNotification.Name("universalLink"), object: nil, userInfo: userInfo)
}
  • sceneWillEnterForeground函数始终被调用
  • scene(scene:continue:)仅在单击通用链接时调用。
  • 我希望NotificationCenter.default.post(name: Notification.Name("update"), object: nil)仅在未单击通用链接时调用。

我尝试在sceneWillEnterForeground中从scene获取userActivity,但没有数据可用于添加任何条件检查。当未单击通用链接时,是否会调用任何其他函数?

xvw2m8pv

xvw2m8pv1#

我通过使用在scene(_:continue:)函数中设置的标志实现了所需的行为,然后检入sceneDidBecomeActive函数,该函数在应用程序返回前台后调用

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var isUniversalLinkOpened = false

    func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
        if let url = userActivity.webpageURL {
            let userInfo: [String: String] = ["url": url.absoluteString]
            NotificationCenter.default.post(name: Notification.Name("universalLink"), object: nil, userInfo: userInfo)
            isUniversalLinkOpened = true
        }
    }

    func sceneDidBecomeActive(_ scene: UIScene) {
        if !isUniversalLinkOpened {
            NotificationCenter.default.post(name: Notification.Name("update"), object: nil)
        }
        isUniversalLinkOpened = false
    }

    // ... other functions ...
}

相关问题