英文:
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 ...
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论