通用链接, continueUserActivity 和 sourceApplication 未被调用

Posted

技术标签:

【中文标题】通用链接, continueUserActivity 和 sourceApplication 未被调用【英文标题】:Universal links, continueUserActivity and sourceApplication not being called 【发布时间】:2017-07-28 09:21:23 【问题描述】:

我终于设法做通用链接,如果我点击链接它将打开我的应用程序。但它不会出现在继续用户活动或源应用程序中。因为我想将它重定向到特定的视图控制器。比如我打开"https://www.myWeb.com/forgotPassword",它会重定向到forgotPasswordViewController,我需要阅读链接。

为什么它不显示在继续用户活动中?

func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool 
    print(url)
    let urlHost : String = url.host as String!
    print("HELLO")
    
        let main : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let inner : ResetPasswordViewController = main.instantiateViewController(withIdentifier: "ResetPasswordViewController") as! ResetPasswordViewController
        self.window?.rootViewController = inner
        self.window?.makeKeyAndVisible()
        return true;
    
   
    


func application(application: UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: ([AnyObject]?) -> Void) -> Bool 
    if userActivity.activityType == NSUserActivityTypeBrowsingWeb 
        let url = userActivity.webpageURL!
        print(url)
        print("HELLO WORLD")
    
    return true

这就是源应用程序发生的情况;谁能告诉我为什么会变成这样?

【问题讨论】:

"https://www.myWeb.com/forgotPassword" - "https://" 和 "www" 之间有一个空格 我添加的空间只是为了能够在这篇文章中显示,实际上我在链接中没有空间 请包含格式为代码而非屏幕截图的代码。 @DavidPasztor 已编辑 您是否实施了“应用程序(didFinishLaunchingWithOptions)”?如果是,那将是使用“launchOptions”调用的函数,其中包含带有“userActivity”的键和用于打开应用程序的“URL”。 【参考方案1】:

已解决 只是因为数据类型,我放了 AnyObject 而不是 Any。 但是编译器没有告诉我是否不能使用 AnyObject

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool 
    print("Continue User Activity: ")
    if userActivity.activityType == NSUserActivityTypeBrowsingWeb 
        let url = userActivity.webpageURL!
        print(url.absoluteString)
        //handle url
    
    return true

【讨论】:

以上是关于通用链接, continueUserActivity 和 sourceApplication 未被调用的主要内容,如果未能解决你的问题,请参考以下文章

iOS 8 上的通用链接?

iOS 9 通用链接(Universal Links)

通用链接 - 域存在一些验证问题

与 Firebase 和 Ionic 的通用链接

带有 Mandrill 子域的通用深度链接

iOS Universal Links(通用链接)