xcode 获取深层链接 url iOS 中的参数值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44580871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 10:03:22  来源:igfitidea点击:

Get value of parameters in deep link url iOS

iosswiftxcodedeeplink

提问by Linar sen

everyone. My question is: How can I get data from deep link URL? I have two apps and I want to send data from app1 to app2 using the deep link. I have a button on app1 to click and open app2 then app 2 will get data from app1 by deep link URL.

每个人。我的问题是:如何从深层链接 URL 获取数据?我有两个应用程序,我想使用深层链接将数据从 app1 发送到 app2。我在 app1 上有一个按钮可以单击并打开 app2,然后 app 2 将通过深层链接 URL 从 app1 获取数据。

Here is my code of button send in app1:

这是我在 app1 中发送按钮的代码:

@IBAction func btnSend_Clicked(_ sender: Any) {
    let text = self.txtInput.text?.replacingOccurrences(of: " ", with: "%20")
    UIApplication.shared.open(URL(string: "myapp://?code=\(text!)")!, options: [:], completionHandler: nil)
}

so, How can i get data from deeplink url (code parameter) in app2?

那么,如何从 app2 中的深层链接 url(代码参数)获取数据?

Really Thanks for your help !!!!

真的很感谢你的帮助!!!!

回答by Sour LeangChhean

You implement this code in Appdelegate:

您在 Appdelegate 中实现此代码:

 func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
        let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false)
        let items = (urlComponents?.queryItems)! as [NSURLQueryItem]
        if (url.scheme == "myapp") {
            var vcTitle = ""
            if let _ = items.first, let propertyName = items.first?.name, let propertyValue = items.first?.value {
                vcTitle = url.query!//"propertyName"
               }
        }
        return false
   }