简体   繁体   English

通过URL发送JSON NSData

[英]Sending a JSON NSData via URL

I am attempting to send a JSON representation of an object in an email link. 我正在尝试通过电子邮件链接发送对象的JSON表示形式。 The recipient will open the link and my app will respond via a url scheme. 收件人将打开链接,而我的应用程序将通过URL方案进行响应。 It must extract the JSON from the url and re-build the object. 它必须从url中提取JSON并重新构建对象。

I am serializing my object by building an NSDictionary and using: 我通过构建NSDictionary并使用以下方法来序列化对象:

return [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:&error];

I'm not sure what comes next. 我不确定接下来会发生什么。 Somehow I need to convert this NSData into a string so that I can prefix my url scheme and use it in a link. 我不知何故需要将此NSData转换为字符串,以便可以在url方案前添加前缀并将其用于链接中。

On the receiving end, I then need to remove the prefix (which I can do) and turn the string back into an NSData . 然后,在接收端,我需要删除前缀(我可以这样做)并将字符串转换回NSData

What is the correct method for doing this? 正确的方法是什么? And how do I make sure that the contents of my data do not interfere with the JSON string encoding (eg if my object contains text including special characters)? 以及如何确保数据的内容不会干扰JSON字符串编码(例如,如果我的对象包含包含特殊字符的文本)?

You need to do an additional encoding step, since there are characters in encoded JSON that also have significance when they are part of a URL. 您需要执行额外的编码步骤,因为编码的JSON中的某些字符在它们作为URL的一部分时也具有重要意义。 What we actually want to do is URL-encode the data so none of the characters in the resulting string conflict with what applications expect a URL to look like. 我们实际上想要做的是数据进行URL编码 ,因此结果字符串中的所有字符都不会与应用程序期望URL的外观发生冲突。

The first step is transforming our data into an NSString (this is basically just a memcpy since NSStrings are encoded in UTF-8 by default): 第一步是将我们的数据转换为NSString (这基本上只是一个memcpy,因为默认情况下, NSStrings是用UTF-8编码的):

NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

Now, there's a function you might be tempted to use called -stringByAddingPercentEscapesUsingEncoding , but it doesn't do a thorough enough job of escaping all the relevant characters, so we need to build our own. 现在,您可能会想使用一个名为-stringByAddingPercentEscapesUsingEncoding ,但是它对转义所有相关字符的工作还不够彻底,因此我们需要构建自己的函数。

I could repeat the code here, but since it's been done many times already, just view this blog that shows how you can add a category to NSString to do proper encoding, after which you can append it and send it on its way. 我可以在这里重复代码,但是由于已经做过很多次了,所以只需查看此博客该博客显示了如何向NSString添加类别以进行正确的编码,然后可以对其进行追加和发送。 Writing the analogous decoding function with CFURLCreateStringByReplacingPercentEscapesUsingEncoding is an exercise for the reader of which many examples can be found floating around. 使用CFURLCreateStringByReplacingPercentEscapesUsingEncoding编写类似的解码功能对于读者CFURLCreateStringByReplacingPercentEscapesUsingEncoding是一个练习,可以发现很多例子。

Make sure your payloads are quite small (on the order of a couple of kB), by the way, since there is probably an upper bound on how long URLs, even those used locally and with a custom scheme, can be. 顺便说一下,请确保您的有效载荷很小(大约为kB数),因为URL(甚至是本地和自定义方案中使用的URL)的长度可能存在上限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM