0

我有一个(非常)基本的通用链接测试程序,可以从链接正确启动到相关域。它将收到的 URL 打印到调试控制台。

在 AppDelegate.swift 中:

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
    // First attempt at handling a universal link
    print("Continue User Activity called: ")
    if userActivity.activityType == NSUserActivityTypeBrowsingWeb,
        let url = userActivity.webpageURL {
        //handle URL
        let u = url.absoluteString
        print(u)
  }
    return true
}

ViewController.swift:

import UIKit

class ViewController: UIViewController {
    
    @IBOutlet weak var result: UILabel!
      
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        print("Ready ..")
        result.text = "view did load"
   }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
}

我只想用收到的 URL 更新 UILabel,以显示它正在工作。

我尝试将 NSUserActivity 处理程序放入 ViewController,它可以编译但不起作用。

我尝试在 ViewController 中创建一个可以从 AppDelegate 调用的方法,但不知道如何解决(我是 Swift 初学者)。

如何获取更新 UILabel 文本的 URL?

4

1 回答 1

1

如果它是根,你可以这样做

if let vc = self.window?.rootViewController as? ViewController {
    vc.result.text = u
}

对于一个完整的解决方案,您应该使用Get top most UIViewController 获得最顶级的 vc, 然后像上面一样投射它

于 2020-10-01T20:42:55.547 回答