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:

Recently, support for Internationalized Domain Names has been getting better, so I bought strö 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];
	if ([pasteboard setString:theUrl forType:NSStringPboardType])
		urlToLoad = [WebView URLFromPasteboard:pasteboard];
@catch (NSException * exception)
	urlToLoad = nil;
		NSLog(@"Can't create URL from string '%@'.", theUrl);
return urlToLoad;[/objc]

For example, your user might type in this URL:
[html]http://www.strö With Spaces/index.html[/html]
Piping that through the above code will yield the following, which you can now plug into an NSURL: