Last night I wasted far too much of my time trying to debug an issue with Windows Phone URI associations and emails containing links using these URIs. Far too much time.

URI associations, for those who haven’t used them, are the ability to register a URI prefix so that any links that use this prefix fire up your application. The possibilities for this are endless and are the basis of app to app communication within the current Windows Phone 8 release. For more information on the basics, there’s a great MSDN article on how to register both URI and file type associations. I’m not going to talk about the basics because that article covers them all. Instead I’m going to concentrate on what caused me to lose hours.

Over the last few evenings, I’ve been mocking up a Windows Phone user interface for an application that I use daily. This is just in a proof of concept state at the moment but I’ve been pulling in various functions available within Windows Phone that would be of use to this application. One of them, of course, was using URI and file type association to show the correct item.

In testing the URI association, I sent myself an email to my Gmail address containing a link that used my prefix. The HTML for the link would have been something like <a href="my-prefix://{guid}">click me!</a>. I diligently added my Gmail account into the emulator’s email accounts, downloaded the email and clicked the link. The application popped up and was passed the GUID. Happy days. Check-in, compile, publish XAP to beta participants, email them to tell them of the wonderful functionality, celebratory curry.

Everything is good in the world.

Now, I’m going to ignore that I should have tested this on my device. Yes, I should have. No, I didn’t.

I get an email that the beta’s been updated, I update and go to check it out. However opening the email on my phone didn’t show my link properly. Instead of being shown in blue and underlined, the link was shown black. Clicking it did nothing. This is the same email, on the same Gmail account, as I tested within the emulator. Even more bizarre is that I get emails from some people saying it works and others saying it doesn’t.

To shorten the story significantly, after a lot of Googling and swearing, I posted on Stack Exchange. It was Matt Lacey that came to the rescue.

Because of the age of my device, my device communicates with Gmail via the Exchange protocol. When the emulator was pointed at Gmail, Google saw it as a new device and forced it down the IMAP route. On the phone, some Exchange policies were being applied, via IMAP they weren’t. I confirmed this by adding Gmail to my device a second time but only as an IMAP source. The links functioned perfectly as they did in the emulator.

First finding: retrieving messages via "Exchange" may do funny things to links containing non-standard URI prefixes.

What’s even more strange is that sending an email with the correct HTML from my work exchange account to my Gmail account, then receiving it via IMAP, also broke the link. The link appeared correctly but clicking it stated that the link didn’t work on the phone and copying the URL just copied the string “inbox”. Sending the same email to myself from my own Gmail account and the link worked.

Second finding: even if Exchange isn’t directly involved in the send/receive, if it’s been involved somewhere then it may break links containing non-standard URI prefixes.

An interesting footnote to add is that Windows Phone will automatically identify links within textual content and make them clickable for users. Most people will have seen this if they receive a text message with a web address or something similar. However, whatever Microsoft are using to identify that string doesn’t seem to work if the URI prefix contains a hyphen – geo:lat,lng will be converted, but my-prefix:abc won’t.

Third finding: if you’re sending links to people containing your non-standard URI prefix then either avoid hyphens in the prefix name (note that several Nokia URI prefixes contain hyphens), or ensure you send HTML emails with the link correctly in the A element’s href attribute.

I hope that helps someone!