Get value of parameters in deep link url iOS

2020-05-24 07:09发布

问题:

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.

Here is my code of button send in 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?

Really Thanks for your help !!!!

回答1:

You implement this code in 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
   }