通用链接打开应用程序但不获取 url 数据

Universal links opening app but not fetching url data

完全按照描述执行了每一种步骤。应用成功打开,但未获取 URL 数据。

完全按照描述执行了每一种步骤。从 gmail 发送 links,然后 links 打开应用程序,是的,它有效。 但是这里有 2 个问题

1- 我到底该如何调试和打印应用程序,因为您必须关闭应用程序并断开应用程序与 Xcode 调试器之间的连接才能从通用 link 重新打开应用程序!!.令人沮丧的是,如果我将应用程序 运行 保留在调试器的后台,通用 link 只是将应用程序状态重新激活到前台,然后功能 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: ....

未调用

2- 无论如何我总是得到 "userActivity?.activityType" = nil。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
 if userActivity?.activityType == NSUserActivityTypeBrowsingWeb {
            let univrsalLinkURL = userActivity?.webpageURL!.absoluteString
//            remoteParmNumTxt = univrsalLinkURL!
            print(univrsalLinkURL!)
        }
return true
}

应该能够捕获 url 和 url 数据 iOS 12.4 Xcode10.3

1- How on earth can I debug and print the app

编辑您的方案并 select "wait for executable to be launched"

然后,实施 func application(:continue:restorationHandler:) 并检查 userActivity.webpageURL 以确定您需要做什么。

有关如何处理通用 links 的苹果文档说要使用与您正在使用的应用程序功能不同的应用程序功能。 didFinishLaunchingWithOptions 只会在应用程序完全关闭时调用一次,从而导致您已经面临的问题。

但是:

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

无论应用是否关闭都会被调用,稍后,当通用 link 被触发时。试试用这个代替。

完整的苹果文档在这里: https://developer.apple.com/documentation/uikit/inter-process_communication/allowing_apps_and_websites_to_link_to_your_content/handling_universal_links

所以在多次尝试获取应用 运行 并获取 url 数据之后 iOS 似乎发生了一些变化,其中 "userActivity" 将始终为 nil,除非您从 "application" 函数

的重载中获取它

所以@Simon McLoughlin 的解决方案通过建议使用这个函数确实解决了一半的问题

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

但是当应用程序已经 运行 在后台运行时 "Only" 会触发此过载。

好吧,如果应用程序是由通用 link 而不是 运行ning 打开的,那么让应用程序获取 url 数据的唯一解决方案如下

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {

    let activityDictionary = launchOptions?[UIApplication.LaunchOptionsKey.userActivityDictionary] as? [AnyHashable: Any] ?? [AnyHashable: Any]()
    let activity = activityDictionary ["UIApplicationLaunchOptionsUserActivityKey"] as? NSUserActivity ?? nil

    if let finalString = activity?.webpageURL?.absoluteString {
        remoteParmNumTxt = finalString
    }

    print("dic",activity?.webpageURL)
    print("yesssss loaded from url")

    return true
}

如果你尝试直接使用 "userActivity" 无论如何它都会给你零值

所以,苹果文档很烂

IOS 版本 > 13.0

仅 运行 个应用程序(来自通用 link)

SceneDelegate.swift

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    ...

    if let activity: NSUserActivity = connectionOptions.userActivities.first,
       let url: URL = activity.webpageURL {
          print(url.absoluteString)
    }

    ...
}