我正在开发可接收Firebase动态链接的应用程序。我想要的是当用户单击动态链接时,应用程序将其重定向到特定的UIViewController
。因此,我的AppDelegate.swift
文件中的代码如下:
@available(iOS 9.0, *)
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any]) -> Bool {
//return GIDSignIn.sharedInstance().handle(url)
return application(app, open: url, sourceApplication: options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String, annotation: "")
}
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
// On progress
if let dynamicLink = DynamicLinks.dynamicLinks().dynamicLink(fromCustomSchemeURL: url) {
print("open url = open dynamic link activity")
print("url = \(dynamicLink)")
let destinationVC = UIStoryboard(name: "DynamicLink", bundle: nil).instantiateViewController(withIdentifier: "DynamicLinkView") as? DynamicLinkVC
self.window?.rootViewController?.navigationController?.pushViewController(destinationVC!, animated: true)
} else {
print("open url = none")
}
return GIDSignIn.sharedInstance().handle(url)
}
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
// On progress
let handled = DynamicLinks.dynamicLinks().handleUniversalLink(userActivity.webpageURL!) { (dynamiclink, error) in
print("dynamic link = \(dynamiclink)")
}
if handled {
let destinationVC = UIStoryboard(name: "DynamicLink", bundle: nil).instantiateViewController(withIdentifier: "DynamicLinkView") as? DynamicLinkVC
self.window?.rootViewController?.navigationController?.pushViewController(destinationVC!, animated: true)
}
return handled
}
因此,当我单击链接时会发生什么情况,该应用程序立即打开,但没有重定向到我想要的所需UIViewController
(在这种情况下为destinationVC
)。它像往常一样直接转到登录页面。但是在调试区域中,链接看起来像这样=
动态链接=可选(https:// xxxx],匹配类型:唯一,minimumAppVersion:不适用,匹配消息:(空)>)
不幸的是,当应用不是由Xcode构建时,我无法记录日志消息。
我对此感到非常困惑,我的代码有什么问题?我是iOS开发的新手,所以不确定在哪里做错了。如果您需要更多信息,请随时提出,我们将为您提供。任何帮助,将不胜感激。谢谢。
答案 0 :(得分:1)
如果您的其余代码工作正常,并且在导航至另一个视图控制器时遇到问题,那么此解决方案将为您服务。
如果您要打开特定的 ViewController ,请在点击动态链接的同时更新用restorationHandler
编写的代码,下面的更新代码将帮助您重定向/导航到特定的View Controller
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
// On progress
let handled = DynamicLinks.dynamicLinks().handleUniversalLink(userActivity.webpageURL!) { (dynamiclink, error) in
print("dynamic link = \(dynamiclink)")
}
if handled {
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "DynamicLink", bundle: nil)
if let initialViewController : UIViewController = (mainStoryboardIpad.instantiateViewController(withIdentifier: "DynamicLinkView") as? DynamicLinkVC) {
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
}
return handled
}
希望这可以解决您的问题。