Universal Windows Dev

How to: Creating URI associations in your Windows Phone app

By 2nd August 2013 One Comment
Windows Phone Logo

Windows Phone Logo

I’ve recently been playing around with some of the features that I haven’t delved into with Windows Phone 8. Most recently I have looked at URI associations and didn’t realise how simple and easy it is to add into your applications.

“URI associations in web apps using a custom protocol”

As many people will know, I am the new online editor for The Hullfire. This means that I get to put together a technology, science and gaming section for our website. I have also been managing their website for the past year voluntarily and developing the official Hullfire app for Windows Phone and soon Windows 8. A good way for me to interact with the website and the Windows Phone app can be achieved using this new URI associations feature in the Windows Phone OS.

URI associations in web apps using a custom protocol, e.g. hullfirewp:id where the ID is the article ID, makes it possible to launch the application and load the article straight away if the user is navigating the website through Internet Explorer or if they are send a link in emails, clicking through social media links and much more!

Now to add this into your application is very easy! Here is how you can add this same functionality to your applications:

1. Open the WMAppManifest.xml file using a text editor than double clicking it which will open the fancy menu for changing certain settings. Then add the following tags below the Tokens section.

<extensions>
  <protocol Name="hullfirewp" NavUriFragment="encodedLaunchUri=%s" TaskID="_default"></protocol>
</extensions>
Where “hullfirewp” will be your unique ID or protocol that will be listening for specific URIs.

2. Create a new class called ‘AssociationUriMapper’ or something along those lines, and add the following code:

using System;
using System.Windows.Navigation;

namespace TheHullfire.Data
{
    public class AssociationUriMapper : UriMapperBase
    {
        private string _temp;
        public override Uri MapUri(Uri uri)
        {
            _temp = System.Net.HttpUtility.UrlDecode(uri.ToString());

            if(_temp.Contains("hullfirewp"))
            {
                // Do task here for your URI association
            }

            return uri;
        }
    }
}
3. Open the App.xaml.cs class file and find the InitializePhoneApplication method. In there change the line of code which initializes the RootFrame to this:
RootFrame = new PhoneApplicationFrame {UriMapper = new AssociationUriMapper()};
And then from there, you can then call the protocol that you have created for your application in the following formats:

  • NFC tags
  • NFC sharing between phones
  • Hyperlink via webpage, email, SMS

Unfortunately, the URI associations protocols don’t support QR code scanning or directly typing in the protocol into Internet Explorer which is a shame for users who want to scan a QR code to unlock something in the application. Hopefully this is something that the Windows Phone team can work on and bring into a new update for the SDK.

If you need any more help with adding URI associations into your application, let me know in the comments section below and I will try my best to help!

Join the discussion One Comment

  • Lindsay Cox says:

    Hey dude, was actually trying to do this today but you have missed out a chunk in this post for the Manifest XML file, I can’t get the phone to actually pick up the uri stuff I need atm

Leave a comment