【问题标题】:Handling Universal Links, in the case of a non-running SwiftUI app在未运行的 SwiftUI 应用程序的情况下处理通用链接
【发布时间】:2020-09-12 05:08:41
【问题描述】:

我正在开发一个处理通用链接的 SwiftUI 应用程序。 在这一点上,它几乎按照预期的方式工作,而我正在通过调试器查看正在发生的事情。换句话说,当我在应用程序处于后台时单击链接时,它会唤醒并且一切正常。

神奇的发生在下面的函数中。

func scene(_ scene: UIScene, 
           continue userActivity: NSUserActivity) {
    print(#function)
    guard userActivity.activityType == NSUserActivityTypeBrowsingWeb,
    let incomingURL = userActivity.webpageURL,
    let components = NSURLComponents(url: incomingURL,
                                     resolvingAgainstBaseURL: true),
    let path = components.path else {return}
    
    let params = components.queryItems ?? [URLQueryItem]()
    
    // ... more useful code for this app ...
    .....
}

但另一方面,如果我在应用程序未运行时单击链接(即使在后台也不运行)。当然不能连接调试器。然后应用程序启动并启动,但没有发生预期的行为(根据链接做出反应)。这是为什么呢?

我认为在这种情况下,我应该处理的情况不是:

func scene(_ scene: UIScene, 
           continue userActivity: NSUserActivity)
           {......}

但我不确定。

非常感谢任何相关的提示。

【问题讨论】:

    标签: ios swift swiftui ios-universal-links


    【解决方案1】:

    当应用打开时,该流程由其他功能处理。您可以从 connectionOptions 获取 URL 或 URL 方案(如果您还处理这种链接)。

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
      if let urlContext = connectionOptions.urlContexts.first {
        handleDeepLinking(url: urlContext.url)
      } else if let userActivity = connectionOptions.userActivities.first, let url = userActivity.webpageURL {
        handleUniversalLinks(url: url)
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-15
      • 2016-03-09
      • 2010-10-05
      • 2016-01-29
      相关资源
      最近更新 更多