The Hit List Diary #22 – URL Scheme

The latest version of The Hit List for iPhone re-instates our custom URL scheme, which gives other apps a means to trigger the creation of new tasks. Here’s a quick example URL for creating a new task:

thehitlist:///inbox/tasks?method=POST&title=example+task

As you might imagine, I thought this an excellent opportunity to have a play with iOS 8’s new NSURLQueryItem class. Mostly it's pretty straightforward, but one quirk of our scheme is that + symbols are specially interpreted as spaces to make the URLs a little more human friendly (lots of websites/services do this, e.g. Google search URLs).

The tricky bit is that NSURLQueryItem itself isn’t very flexible about encoding. It considers + to be a valid character, which doesn’t need any special handling, and is interpreted as a plus symbol, not a space.

To handle all the possibilities correctly, I wracked my brains a bit and came up with a cunning bit of pre-processing:

If you want to go in the other direction and create such a URL, a similar approach can be employed:

In a quest for neatness, you could go even further, and post-process the query again to re-encode spaces (%20) as plus symbols. But it would be just that: neatness; no actual functional change.

© Mike Abdullah 2007-2015