iOS sceneWillEnterForeground 处理通用链接

huangapple go评论67阅读模式
英文:

IOS sceneWillEnterForeground Handle Universal links

问题

Here are the translated parts:

  • sceneWillEnterForeground function is always called: "sceneWillEnterForeground"函数总是被调用。
  • scene(scene:continue:) is called only when universal link is clicked: "scene(scene:continue:)"函数仅在通用链接被点击时被调用。
  • I want NotificationCenter.default.post(name: Notification.Name("update"), object: nil) to only be called when universal link is not clicked: 我希望 NotificationCenter.default.post(name: Notification.Name("update"), object: nil) 仅在通用链接未被点击时调用。

As for your last question, "Is there any other function that would be called when universal links is not clicked?", it's not a direct translation, but I can provide the translation for it: "是否有其他函数在未点击通用链接时被调用?"

英文:

In my ios app I have below functions in my SceneDelegate class:

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 function is always called
  • scene(scene:continue:) is called only when universal link is clicked.
  • I want NotificationCenter.default.post(name: Notification.Name("update"), object: nil) to only be called when universal link is not clicked.

I tried getting userActivity from scene in sceneWillEnterForeground but no data is present to add any conditional checks. Is there any other function that would be called when universal links is not clicked?

答案1

得分: 0

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 ...
}
英文:

I achieved the desired behavior by using a flag that is set in the scene(_:continue:) function, and then checked in the sceneDidBecomeActive function, which is called after the app has returned to the foreground

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 ...
}

huangapple
  • 本文由 发表于 2023年3月23日 12:02:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75819164.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定