通过swift中的url方案在两个Apps之间传递数据? [英] Pass data between two Apps by url scheme in swift?

查看:129
本文介绍了通过swift中的url方案在两个Apps之间传递数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两个测试应用程序称为Sender&接收方

There are two test Apps called Sender & Receiver

它们通过Url Scheme相互通信。我想从Sender发送一个字符串给Receiver,这可能吗?

They communicate with each other by Url Scheme. I would like to send a String from Sender to Receiver, is that possible?

字符串的详细信息:

我都在Sender和Receiver中创建Textfields,我会在Sender Textfield上发一些字符串。单击按钮时,字符串将显示在Receiver Textfield上。

I both create Textfields in Sender and Receiver, I would text some String on Sender Textfield. When I click button, the String will show on the Receiver Textfield.

我似乎必须在我的应用接收器中实现NSNotificationCenter.defaultCenter()。postNotificationName

这是我的App Receiver代码:

Here is my App Receiver code:

在Appdelegate中

In Appdelegate

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {

    calledBy = sourceApplication
    fullUrl = url.absoluteString
    scheme = url.scheme
    query = url.query
}

在viewController中

In viewController

override func viewDidLoad() {
    super.viewDidLoad()

    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.displayLaunchDetails), name: UIApplicationDidBecomeActiveNotification, object: nil)
    // Do any additional setup after loading the view, typically from a nib.
}

func displayLaunchDetails() {
    let receiveAppdelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    if receiveAppdelegate.calledBy != nil {
        self.calledByText.text = receiveAppdelegate.calledBy
    }
    if receiveAppdelegate.fullUrl != nil {
        self.fullUrlText.text = receiveAppdelegate.fullUrl
    }
    if receiveAppdelegate.scheme != nil {
        self.schemeText.text = receiveAppdelegate.scheme
    }
    if receiveAppdelegate.query != nil {
        self.queryText.text = receiveAppdelegate.query
    }
}

现在,我只能显示有关网址的信息像这样

Now, I only can show the information about the url like this

希望得到一些建议!

推荐答案

是的,你可以使用查询string。

Yes, you can use query string.

url.query包含查询字符串。例如,在URL
iOSTest://www.example.com/screen1?textSent =Hello World中,查询字符串为 textSent =Hello World

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".

通常我们也使用URLSchemes进行深层链接,因此URLScheme指定要打开的应用程序,并且url中的路径指定要打开哪个屏幕并查询字符串我们想要发送给应用程序的其他参数。

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 是一个字符串,因此您必须解析它才能获得所需的值:
例如,在URL iOSTest://www.example.com/screen1?key1 = value1& key2 = value2中,查询字符串是key1 = value1& key2 = value2。我正在编写代码来解析它,但请确保根据您的情况进行测试:

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将包含查询字符串中的所有键值对。
希望它有所帮助:]

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

如果您不想进行深度链接,可以直接将queryString附加到scheme。例如:iOSTest://?textSent =Hello World

这篇关于通过swift中的url方案在两个Apps之间传递数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆