Fork me on GitHub

IDNs and Cocoa

My family name contains an umlaut, a fact that sometimes leads to confusion in the English-speaking world, especially when it comes to data-entry on shoddily coded websites. All my life, I have habitually used Stroeck instead of my actual name, and that’s also my personal domain: stroeck.com.

Recently, support for Internationalized Domain Names has been getting better, so I bought ströck.com. The first thing I did, of course, was typing it into Vienna’s browser bar. Needless to say, it just failed silently :-) NSURL cannot handle IDNs, so in Vienna we now use a little hack to parse them. As far as I’m aware, WebView’s URLFromPasteboard: is the only way to get this functionality in Cocoa without using private API or JavaScript trickery, which I was using before Jeff Johnson found this solution:

NSURL *urlToLoad = nil;
NSPasteboard * pasteboard = [NSPasteboard pasteboardWithName:@"ViennaIDNURLPb"];
[pasteboard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
@try
{
	if ([pasteboard setString:theUrl forType:NSStringPboardType])
		urlToLoad = [WebView URLFromPasteboard:pasteboard];
}
@catch (NSException * exception)
{
	urlToLoad = nil;
	{
		NSBeep();
		NSLog(@"Can't create URL from string '%@'.", theUrl);
	}
}
return urlToLoad;

For example, your user might type in this URL:

http://www.ströck.com/Folder With Spaces/index.html

Piping that through the above code will yield the following, which you can now plug into an NSURL:

http://www.xn--strck-lua.com/Folder%20With%20Spaces/index.html