委托方法中无法检测到iOS Universal Link

问题描述

我目前正在我的应用上测试通用链接。我已经上传了apple-app-site-association,编辑了我的应用程序的权利文件。当我单击注释中的链接(例如:domain.com/test/id12345)时,可以打开该应用程序。

但是,它只会打开应用程序,而是从应用程序本身打开。捕获信息的委托方法不会被调用

该项目是带有iOS 13的SwiftUI项目,具有UIKit App Delegate的生命周期。

在AppDelegate.swift下:

    func application(_ application: UIApplication,continue userActivity: NSUserActivity,restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
  
      print("Test: this is never called:\(userActivity)")
      
      guard userActivity.activityType == NSUserActivityTypebrowsingWeb,let url = userActivity.webpageURL,let components = URLComponents(url: url,resolvingAgainstBaseURL: true) else {
          return false
      }
      return false
    }

有人知道如何检测单击的通用链接吗?我应该如何调试它,该链接确实会打开该应用程序,但其余部分我无法处理。

解决方法

如果您的应用程序支持多窗口,或者您没有销毁SceneDelegate,则需要在SceneDelegate中实现此方法

// SceneDelegate.swift

func scene(_ scene: UIScene,openURLContexts URLContexts: Set<UIOpenURLContext>) {
    _ = LoginManager.shared.application(.shared,open: URLContexts.first?.url)
}