gpt4 book ai didi

ios - 快速通过 url 方案在两个应用程序之间传递数据?

转载 作者:IT王子 更新时间:2023-10-29 05:36:12 25 4
gpt4 key购买 nike

有两个测试 App,分别叫做 Sender & Receiver

它们通过 Url Scheme 相互通信。我想从发送方向接收方发送一个字符串,这可能吗?

关于字符串的详细信息:

我都在 Sender 和 Receiver 中创建文本字段,我会在 Sender 文本字段上发送一些字符串。当我单击按钮时,字符串将显示在接收方文本字段上。

It seems that I have to implement NSNotificationCenter.defaultCenter().postNotificationName in my Apps Receiver

这是我的 App Receiver 代码:

在 Appdelegate 中

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

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

在 View Controller 中

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
}
}

现在,我只能显示关于 url like this 的信息

image2

希望得到一些建议!

最佳答案

是的,您可以使用查询字符串。

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

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

url.query 是一个字符串,因此您必须解析它以获得您需要的值:例如,在 URL iOSTest://www.example.com/screen1?key1=value1&key2=value2 中,查询字符串是 key1=value1&key2=value2。我正在编写代码来解析它,但请确保针对您的情况对其进行测试:

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

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

关于ios - 快速通过 url 方案在两个应用程序之间传递数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36688838/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com