0

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. My app would be able to compose the email, and embed a link using this url scheme. 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. 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...

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?

Ben Packard
  • 26,102
  • 25
  • 102
  • 183
  • 1
    You will need to percent-escape the fields in your URL fields (e.g. replace ampersand with `%26`, etc.). But before you go there, note that generally URLs have some practical limitation on length, so sending a full recipe (esp with a picture, which you'd have to base64-encode or something like that) may not be practical. – Rob Jan 01 '13 at 22:25
  • 1
    See http://stackoverflow.com/questions/705448/iphone-sdk-issue-with-ampersand-in-the-url-string for discussions about percent escaping your parameters, if you end up going that route. – Rob Jan 01 '13 at 22:31
  • Thanks. What would happen when I decode, and the user has entered %26 in the name? – Ben Packard Jan 01 '13 at 22:48
  • 1
    The % itself, will get percent encoded. – Rob Jan 02 '13 at 00:31

1 Answers1

1

You should sanitize (i. e. URL-escape) the user input before embedding it to the URL. You can use the CoreFoundation function CFURLCreateStringByAddingPercentEscapes() function for this kind of conversion.

  • I think I get it. So should I encode each ingredient name individually, then form the string? On the receiving end, I would then separate out the ingredients, and decode each? – Ben Packard Jan 01 '13 at 22:52
  • @BenPackard Yes, that's it, basically. Also, don't forget that using the same name/key for different ingerdients is not good practice, come up with something else instead of `&ing=first ingredient&ing=second ingredient`, etc. –  Jan 01 '13 at 22:53
  • I was wondering about that. Maybe comma-separated ingredients? E.g. ingredients=firstIng,secondIng, etc? Except, I guess I need to pick something that will be encoded actually? – Ben Packard Jan 01 '13 at 22:56