gpt4 book ai didi

ios - 有没有人有好主意来处理带有应该显示单页的 id 的深层链接?

转载 作者:行者123 更新时间:2023-11-30 11:05:35 25 4
gpt4 key购买 nike

有人有处理深层链接的好主意吗?我想将需要 id 从 HomeViewcontroller (或任何都可以)的单个页面 View 推送到具有从深层链接获取的 id 的单个页面。

我目前的情况是,我可以通过如下方式获取AppDelegate文件中的深层链接及其内部的id。

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
if let incomingURL = userActivity.webpageURL {
let linkHandled = DynamicLinks.dynamicLinks()!.handleUniversalLink(incomingURL) { [weak self](dynamiclink, error) in
guard let strongSelf = self else { return }
if let dynamiclink = dynamiclink, let _ = dynamiclink.url {
strongSelf.handleIncomingDynamicLink(dynamicLink: dynamiclink)
}
}
return linkHandled
}
return false
}

func handleIncomingDynamicLink(dynamicLink: DynamicLink) {
guard let pathComponents = dynamicLink.url?.pathComponents else {
return
}

if pathComponents.count > 1 {

for (i, value) in pathComponents.enumerated() {
if i == 1 {
// define whether the deeplink is for topic or post
UserDefaults.standard.set(value, forKey: "deepLinkType")
print(value)
} else if i == 2 {
UserDefaults.standard.set(value, forKey: "deepLinkId")
print(value)
}
}
}

}

然后在HomeViewController中viewDidAppear

        if (self.isViewLoaded && (self.view.window != nil)) {
let us = UserDefaults.standard

if let deepLinkType = us.string(forKey: "deepLinkType"), let deepLinkId = us.string(forKey: "deepLinkId"){

us.removeObject(forKey: "deepLinkType")
us.removeObject(forKey: "deepLinkId")

if deepLinkType == "topic" {

let storyboard = UIStoryboard(name: "Topic", bundle: nil)
let nextVC = storyboard.instantiateViewController(withIdentifier: "SingleKukaiVC") as! TopicViewController
nextVC.topicKey = deepLinkId

self.navigationController?.pushViewController(nextVC, animated: true)

} else if deepLinkType == "post" {

}
}
}

当应用程序既不在前台也不在后台时,这可以正常工作,我的意思是如果它没有实例化。然而,当应用程序被实例化时,这不起作用,因为 viewDidAppear 不会被读取。或者,如果用户打开了另一个 View ,甚至可能不会调用 HomeViewController 本身。

所以我的问题是,处理具有单页 id 的深层链接的最佳方法是什么?我很欣赏一些例子。

提前致谢。

最佳答案

不要编写代码来在 HomeViewController 中推送 Topic View Controller 。

在App Delegate的handleIncomingDynamicLink方法中,获取最顶层的 View Controller ,然后创建 View Controller (如您的代码中所示),然后从最顶层的 View Controller 推送它。

Your code to create Topic View Controller:
let storyboard = UIStoryboard(name: "Topic", bundle: nil)
let nextVC = storyboard.instantiateViewController(withIdentifier: "SingleKukaiVC") as! TopicViewController
nextVC.topicKey = deepLinkId

检查URL了解如何获取最顶层的 View Controller

关于ios - 有没有人有好主意来处理带有应该显示单页的 id 的深层链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52774056/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com