【问题标题】:iOS, Universal links, Swift. continueUserActivity not callingiOS、通用链接、Swift。 continueUserActivity 没有调用
【发布时间】:2017-05-02 16:01:55
【问题描述】:

我正在为我们的 iOS 应用程序实现通用链接。

这是我的 AppDelegate 的一小部分:

private func application(_ application: UIApplication, openURL url: URL, sourceApplication: String?, annotation: AnyObject) -> Bool {
    DeepLinkHelpers.handleUniversalLink(url.absoluteString)
    return true
}

private func application(application: UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: ([AnyObject]?) -> Void) -> Bool {
    DeepLinkHelpers.handleUniversalLink(userActivity.webpageURL?.absoluteString)
    return true
}

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    signalRConnector.launch()

    NotificationCenter.default.addObserver(self, selector: #selector(AppDelegate.processRestartSignalRNotification(_:)), name: NSNotification.Name(rawValue: "restartSignalR"), object: nil)

    NotificationCenter.default.addObserver(self,                            selector: #selector(AppDelegate.reachabilityChanged(_:)),
                                           name: ReachabilityChangedNotification,
                                           object: reachability)

    do {
        try reachability.startNotifier()
    } catch {
        Logger.save(text: "Unable to start notifier")
    }
    return true
}

我已经处理了通用链接集成的所有其他步骤:

  1. 在我们的 Web 应用程序中发布了 apple-app-site-association 文件
  2. 在 developers.apple.com 上启用关联域功能
  3. xcode 中指定的关联域
  4. 已检查权利文件的目标成员身份
  5. 说 xcode 等到应用程序将手动启动(而不是 自动启动)

我正在做以下调试:

  1. 连接ipad
  2. 在 xcode 中启动项目
  3. 在 ipad 打开日历并单击某些事件中包含的链接。 链接格式如下:app.domain.com/#/123456789
  4. Ipad 打开应用程序,但 continueUserActivity 没有调用,我不能 处理来自 url 的代码,以便导航到应用中的确切状态。

根据文档 continueUserActivity 应该被执行。当应用程序在后台运行和应用程序未运行时,它都不会执行。

提前感谢您!任何帮助表示赞赏。

【问题讨论】:

标签: ios swift ios-universal-links


【解决方案1】:

从 iOS 12、Swift 4.2 和 Xcode 10 开始,Apple 再次将方法签名(restoreHandler 的类型)更改为

 func application(_ application: UIApplication, continue userActivity: 
NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {

【讨论】:

  • 自我注意:在创建新版本之前将警告视为错误,Kristof。
【解决方案2】:

我怀疑这是因为您已将 continueUserActivity 函数标记为 private。我从来没有见过,事实上 Swift 3 在我尝试时实际上给出了一个错误。似乎没有任何以这种方式构造的代码示例on all of GitHub

我建议删除 private 范围。

【讨论】:

  • 谢谢亚历克斯!我已将代码更改为: func application(_ application: UIApplication, continue userActivity: NSUserActivity, restoreHandler: @escaping ([Any]?) -> Void) -> Bool { ... return true } 现在可以使用了。
  • 谢谢亚历克斯,一定是抄了一个假教程。
【解决方案3】:

在 2019 年,这对我有帮助(来自本指南:https://medium.com/@abhimuralidharan/universal-links-in-ios-79c4ee038272):

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-12
    • 1970-01-01
    • 1970-01-01
    • 2020-01-06
    • 2017-01-11
    • 2017-02-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多