Using WebKit to encode unescaped URL strings

A support case came up recently about IDNs (Internationalized Domain Names). I wondered how much work it would be to support them in Sandvox and cast around the web. About all I could find on the subject was a blog post from the Vienna RSS reader team.

They hatched a delightfully devious plan to tunnel into WebKit's capabilities for this, with the bonus that it also takes care of escaping characters elsewhere in the URL. I decided to adopt the same approach, adjusting it slightly:

There's not too many changes to be honest:

I'm using +pasteboardWithUniqueName because it seems a little neater and more generic. (Before some smart-ass comments, I'm not using dispatch_once because this code only runs on the main thread)

Adoption of the 10.6+ pasteboard API.

It would be surprising if WebKit is actually throwing an exception when dealing with some URL strings. Sadly the Vienna authors don't comment if this was a precaution on their part, or necessary. I decided to remove the @try block. If we do get reports of WebKit doing this, I'll update this post/gist.

Of course it would be nice to see Foundation provide its own proper IDN support. Filed as rdar://problem/12679313

Some alternatives:


I'll update the list if I come across any others.

© Mike Abdullah 2007-2013