Pass data between two Apps by url scheme in swift?

我与影子孤独终老i 提交于 2019-12-04 02:57:39

Yes, you can use query string.

url.query contains the query string. For example, in the URL iOSTest://www.example.com/screen1?textSent="Hello World", the query string is textSent="Hello World".

Normally we use URLSchemes for deeplinking too, therefore the URLScheme specifies which app to open and the path in the url specifies which screen to open and query string has additional parameters which we want to send to the app.

url.query is a string, therefore you will have to parse it to get the value you need: For example, in the URL iOSTest://www.example.com/screen1?key1=value1&key2=value2, the query string is key1=value1&key2=value2. I'm writing code to parse it but make sure you test it for your case:

    let params = NSMutableDictionary()
    let kvPairs : [String] = (url.query?.componentsSeparatedByString("&"))!
    for param in  kvPairs{
        let keyValuePair : Array = param.componentsSeparatedByString("=")
        if keyValuePair.count == 2{
            params.setObject(keyValuePair.last!, forKey: keyValuePair.first!)
        }
    }

params will contain all key value pairs in query string. Hope it helps :]

In case you don't want to do deep-linking, you can directly append the queryString to scheme. eg: iOSTest://?textSent="Hello World"

Of course you can. You just compose your app launch URL and pass the parameters like this

iOSTest://?param1=Value1&param2=Valuew

and then parse it in UIApplicationDelegate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!