在 Xcode 14 中将自定义 URL 方案数据从 AppDelegate 传递到 ViewController

Pass custom URL scheme data from AppDelegate to ViewController in Xcode 14

提问人:鄭小火 提问时间:9/15/2023 更新时间:9/15/2023 访问量:43

问:

自定义 URL 方案启动我的应用程序,这部分有效! 现在,我需要将自定义 URL 方案数据从 AppDelegate 传递给 ViewController 它总是一个空字符串,为什么?

在 AppDelegate 中:

    var window: UIWindow?
    var mytr: String!
    
    func application(_ app: UIApplication,
                     open url: URL,
                     options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {

        mytr = "TheData" //just try a string first
        return true
    }

在 ViewController 中

        override func viewDidLoad() {
        super.viewDidLoad()
        self.webView.uiDelegate = self
        
        let appDelegate = UIApplication.shared.delegate as? AppDelegate
        let mypath = appDelegate?.mytr //here mypath always be "" why?
Swift Xcode AppDelegate URL 方案

评论

0赞 vadian 9/15/2023
显然总是在之前调用.使用回调关闭或发布通知。viewDidLoadopen url
0赞 鄭小火 9/15/2023
我怎么知道viewDidLoad中的open-url?
0赞 vadian 9/15/2023
使用调试器记录当前时间,以证明首先可靠地调用了当前时间。viewDidLoad
0赞 鄭小火 9/15/2023
是否可以直接在 viewDidLoad 函数中而不是从委托中获取 open-url(例如 myscheme://myparam)?
0赞 vadian 9/15/2023
否,因为 delegate 方法始终在符合UIApplicationDelegate

答: 暂无答案