URI associations for Windows Phone
This article explains how to associate your application with an uri format in order to launch it when such uri is detected
Note: This is a community entry in the Windows Phone 8 Wiki Competition 2012Q4.
Windows phone 8 introduced the possibility for your app to register to a predefined kind of URI. This will allow you to launch your application from various sources and with various parameters. The URI will have to be formatted as follow
"<Custom Protocol Name>" : "<The landing page>""<Some optional parameters if needed>"
Note that its not mandatory to put the landing page after the semi colon, but it is a good practice. You can use an Id like this :
How to register for an uri association
To register an uri for your app you will have to manually edit your manifest. In the solutions explorer open the properties folder, right click on the WMAppManifest.xml and select View Code
Scroll down to the end of the Tokens element and add an Extensions element with a Protocol children like this:
<Protocol Name="myappuri" NavUriFragment="encodedLaunchUri=%s" TaskID="_default" />
Just replace myappuri' by the uri you want to use. The other parameters are mandatory and must stay like this.
How to detect that your user launched the app from the uri association
You will have to implement a custom uri mapper class that will parse the Uri at the launch of your application and try to detect the source.
Following the sample provided above, the UriMapper would look like this in our case
class AssociationUriMapper : UriMapperBase
private string tempUri;
public override Uri MapUri(Uri uri)
tempUri = System.Net.HttpUtility.UrlDecode(uri.ToString());
// URI association launch for my app detected
// Get the category (after "Category=").
int categoryIndex = tempUri.IndexOf("Category=") + 9;
string category = tempUri.Substring(categoryIndex);
// Redirect to the MainPage.xaml with the proper category to be displayed
return new Uri("/MainPage.xaml?Category=" + category, UriKind.Relative);
// Otherwise perform normal launch.
One more step to ensure that your application will use your new UriMapper, go to App.xamls.cs file in the InitializePhoneApplication() method and add this line
RootFrame.UriMapper = new AssociationUriMapper();
Your app will now try to detect if it was launched from the normal way or from uri association, and in this case it will parse the parameters provided and redirect to the proper page.
What will and will not work
Disclamer, this is based on personnal experimentation. I do not say that all of this will never worked but from what I was able to test it is currently not working.
What will trigger your uri association and launch your app:
- Get the uri from NFC tag
- Get the uri by mail
- Click on a html link containing the uri
- Webpage redirection with a custom protocol
- Get the uri from NFC device sharing an uri (e.g. PublishUriMessage(Uri) from ProximityDevice class on WP8)
What will NOT trigger your uri association and launch your app:
- Enter directly the uri in the webbrowser
- Get the uri by SMS
- Scan a QR code from bing search
What if many applications registered the same name
In this case the user will be prompted to select which application he want to launch