简体   繁体   English

使用自定义URL方案导入数据

[英]Using a custom URL scheme to import data

I would like a simple way for users of my app to send content to each other. 我想为我的应用程序的用户提供一种相互之间发送内容的简单方法。 This content will be a single recipe, with a name, list of ingredients, and some instructions. 该内容将是一个食谱,并带有名称,成分列表和一些说明。 Maybe one day it will include some images. 也许有一天它将包含一些图像。

I am drawn to the simplicity of using a custom URL scheme to achieve this, and relying on emails for communication. 我被吸引到使用自定义URL方案来实现此目标并依靠电子邮件进行通信的简单性。 My app would be able to compose the email, and embed a link using this url scheme. 我的应用程序将能够撰写电子邮件,并使用此url方案嵌入链接。 The person receiving the recipe, would click this link in the email and it would open my app (if installed). 收到食谱的人将单击电子邮件中的此链接,这将打开我的应用程序(如果已安装)。 The link would include the details of the recipe to be added. 该链接将包含要添加的配方的详细信息。

I am not sure how best to construct the URL so that it contains the details of the recipe. 我不确定如何最好地构造URL,以使其包含配方的详细信息。 Whichever convention I employ to identify the recipe name vs ingredients vs instructions, is likely to be flakey. 无论我采用哪种惯例来标识配方名称,成分还是说明,都可能是胡扯。 For example, if I decide to construct something like: 例如,如果我决定构造类似的东西:

`myApp://name=salsa&ing=tomatoes&ing=garlic... `myApp:// name = salsa&ing = tomatoes&ing = garlic ...

if a user has created a recipe using '&' in an ingredient name, I will parse the url incorrectly. 如果用户在配料名称中使用“&”创建了配方,则我将错误地解析网址。

How might I avoid this? 我如何避免这种情况? Is there someway to package the contents first, or maybe include a checksum at least (so the receiving app can abandon anything that doesn't look right). 是否有办法先打包内容,或者至少包含一个校验和(以便接收应用程序可以放弃看起来不正确的任何内容)。 And if not, is there a best practice for constructing a URL to contain data like this? 如果不是,是否存在构建包含此类数据的URL的最佳实践?

You should sanitize (ie URL-escape) the user input before embedding it to the URL. 在将用户输入嵌入URL之前,应先对其进行清理(即URL转义)。 You can use the CoreFoundation function CFURLCreateStringByAddingPercentEscapes() function for this kind of conversion. 您可以使用CoreFoundation函数CFURLCreateStringByAddingPercentEscapes()函数进行这种转换。

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

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