通用链接重定向到特定视图
Universal Links redirection to specific view
我很难理解通用 Link 的工作原理。
首先,我尝试从 SceneDelegate 重定向到特定的 UIViewController(通用 link 工作,因为它将用户从电子邮件中的按钮单击重定向到我的应用程序),但它不起作用。
我还注意到 URL 没有在 appDelegate 中处理,我不知道为什么。
这是 AppDelegate 中函数的片段:
func application(_ application: UIApplication,
continue userActivity: NSUserActivity,
restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool
{
print("am I even here ?")
guard userActivity.activityType == NSUserActivityTypeBrowsingWeb,
let incomingURL = userActivity.webpageURL,
let components = NSURLComponents(url: incomingURL, resolvingAgainstBaseURL: true) else {
return false
}
// Check for specific URL components that you need.
guard let path = components.path,
let params = components.queryItems else {
return false
}
print("path = \(path)")
if let userId = params.first(where: { [=10=].name == "_id" } )?.value,
let token = params.first(where: { [=10=].name == "token" })?.value {
print("_id = \(userId)")
print("token = \(token)")
return true
} else {
print("Either INDEX ARE MISSING")
return false
}
}
来自 SceneDelegate 的片段:
func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
// the url is going to be in userActivity.webpageURL
print(userActivity.webpageURL)
print(userActivity.webpageURL?.pathComponents[2])
print(userActivity.webpageURL?.pathComponents[1])
if (userActivity.webpageURL?.lastPathComponent != nil ) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = self.window?.rootViewController?.storyboard?.instantiateViewController(withIdentifier: "ViewController") as? ViewController
//queryVC?.urlQueryId = queryId!
self.window?.rootViewController?.present(vc!, animated: true, completion: nil)
print("am I moving ?")
}
}
提前抱歉,我对 swift 和 IOS 总体上还是新手,感觉我陷入了一个循环。
ps:来自 SceneDelagate 打印的数据,我得到了两个参数和完整的 URL 以及参数。
谢谢,
好的,这已修复,从 ios 13 开始,如果您有场景委托,那么您应该在此处处理响应。
我就是这样修复它的。
if (userActivity.webpageURL?.pathComponents[2] != nil)
{
let userId = userActivity.webpageURL?.pathComponents[1]
let token = userActivity.webpageURL?.pathComponents[2]
if self.window?.rootViewController?.presentedViewController != nil {
self.window?.rootViewController?.dismiss(animated: false, completion: nil)
}
let Main = self.window?.rootViewController?.storyboard?.instantiateViewController(withIdentifier: "ResetPassword") as? ResetPasswordViewController
//Main.userIdentification = "fd"
Main?.userIdFromUrl = userId
Main?.UsertokenFromUrl = token
// Main.userToken = "aa"
Main?.modalPresentationStyle = .fullScreen
self.window?.rootViewController?.present(Main!, animated: true, completion: nil)
}
else {print("doesn't contain")}
userIdFromUrl 和 UsertokenFromUrl 也是我在目标视图控制器中初始化的变量。
我希望这对任何人都有帮助。很少有教程提到场景委托的使用,在阅读了十几个之后,我最终 运行 圈子里。
您还可以阅读更多相关信息:
https://developer.apple.com/documentation/xcode/supporting-universal-links-in-your-app
我很难理解通用 Link 的工作原理。 首先,我尝试从 SceneDelegate 重定向到特定的 UIViewController(通用 link 工作,因为它将用户从电子邮件中的按钮单击重定向到我的应用程序),但它不起作用。 我还注意到 URL 没有在 appDelegate 中处理,我不知道为什么。
这是 AppDelegate 中函数的片段:
func application(_ application: UIApplication,
continue userActivity: NSUserActivity,
restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool
{
print("am I even here ?")
guard userActivity.activityType == NSUserActivityTypeBrowsingWeb,
let incomingURL = userActivity.webpageURL,
let components = NSURLComponents(url: incomingURL, resolvingAgainstBaseURL: true) else {
return false
}
// Check for specific URL components that you need.
guard let path = components.path,
let params = components.queryItems else {
return false
}
print("path = \(path)")
if let userId = params.first(where: { [=10=].name == "_id" } )?.value,
let token = params.first(where: { [=10=].name == "token" })?.value {
print("_id = \(userId)")
print("token = \(token)")
return true
} else {
print("Either INDEX ARE MISSING")
return false
}
}
来自 SceneDelegate 的片段:
func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
// the url is going to be in userActivity.webpageURL
print(userActivity.webpageURL)
print(userActivity.webpageURL?.pathComponents[2])
print(userActivity.webpageURL?.pathComponents[1])
if (userActivity.webpageURL?.lastPathComponent != nil ) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = self.window?.rootViewController?.storyboard?.instantiateViewController(withIdentifier: "ViewController") as? ViewController
//queryVC?.urlQueryId = queryId!
self.window?.rootViewController?.present(vc!, animated: true, completion: nil)
print("am I moving ?")
}
}
提前抱歉,我对 swift 和 IOS 总体上还是新手,感觉我陷入了一个循环。
ps:来自 SceneDelagate 打印的数据,我得到了两个参数和完整的 URL 以及参数。
谢谢,
好的,这已修复,从 ios 13 开始,如果您有场景委托,那么您应该在此处处理响应。 我就是这样修复它的。
if (userActivity.webpageURL?.pathComponents[2] != nil)
{
let userId = userActivity.webpageURL?.pathComponents[1]
let token = userActivity.webpageURL?.pathComponents[2]
if self.window?.rootViewController?.presentedViewController != nil {
self.window?.rootViewController?.dismiss(animated: false, completion: nil)
}
let Main = self.window?.rootViewController?.storyboard?.instantiateViewController(withIdentifier: "ResetPassword") as? ResetPasswordViewController
//Main.userIdentification = "fd"
Main?.userIdFromUrl = userId
Main?.UsertokenFromUrl = token
// Main.userToken = "aa"
Main?.modalPresentationStyle = .fullScreen
self.window?.rootViewController?.present(Main!, animated: true, completion: nil)
}
else {print("doesn't contain")}
userIdFromUrl 和 UsertokenFromUrl 也是我在目标视图控制器中初始化的变量。 我希望这对任何人都有帮助。很少有教程提到场景委托的使用,在阅读了十几个之后,我最终 运行 圈子里。
您还可以阅读更多相关信息:
https://developer.apple.com/documentation/xcode/supporting-universal-links-in-your-app