URI associations for Windows Phone
This article explains how to associate your application with an URI scheme in order to launch it when interaction with such URI is detected.
See Also
- URI associations for Windows Phone
- URI Association Schemes List (known schemes)
- Designing URI scheme handlers
- URI schemes for launching built-in apps for Windows Phone 8
- Reserved URI scheme names
- Windows Phone DevCenter URI Association Sample
Article Metadata
Code Example
Compatibility
Article
Introduction
Windows Phone 8 introduces the possibility for your app to register to a predefined kind of URI scheme. This will allow you to launch your application from various sources and with various optional parameters. The URI will have to be formatted as follows:
<Custom Protocol Name>:<The landing page>?[First parameter name]=[First parameter value]&[Second parameter name]=[Second parameter value]
For example:
myappuri:MainPage?Category=5&Subcategory=3
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:
myappuri:45645645645
How to register for an URI association
To register an URI scheme for your app you will have to manually edit the app manifest. In the Solution Explorer open the Properties folder, right click on the WMAppManifest.xml file and select View Code. Scroll down to the end of the Tokens element and add an Extensions element with a Protocol children like this:
<Extensions>
<Protocol Name="myappuri" NavUriFragment="encodedLaunchUri=%s" TaskID="_default" />
</Extensions>
Just replace myappuri value with the URI you want to use. The other parameters are mandatory and must stay like this.
What URI scheme should I use?
To ensure you don't accidentally re-use a URI reserved by the operating system or default applications or a URI registered to another third party app, we recommend you include your company and app name in your URI as shown (see Designing URI scheme handlers for more detail):
<companyName>-<nameOfApp>
Developers can also create and use protocols that are intended for general "classes" of apps. For example, developers of Calendar apps might agree a common protocol, and allow end-users to determine what app they choose to download or use as the handler. Note that there is nothing to stop developers using any protocol that isn't reserved for the default apps/operating system.
How to detect that your user launched the app from the URI association
You will have to implement a custom UriMapper 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
if (tempUri.Contains("myappuri:MainPage?Category="))
{
// 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.
return uri;
}
}
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 the URI association, and in this case it will parse the parameters provided and redirect to the proper page.
How to retrieve the parameters once you have been redirected
Like for any page navigation in Windows Phone you'll have to check the QueryString in NavigationContext once you've reached your target page to retrieve your parameters:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (NavigationContext.QueryString.ContainsKey("Category"))
{
Category = int.Parse(NavigationContext.QueryString["Category"]);
}
base.OnNavigatedTo(e);
}
What will and will not work
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).
What will NOT trigger your URI association and launch your app
- Enter directly the URI in the Web Browser.
- Scan a QR code from Bing search.
What MAY trigger your URI association and launch your app
- Get the URI by SMS. It seems to depend on the uri your registered and could be connected to its length
What if many applications registered the same name
In this case the user will be prompted to select which application he want to launch
Inter-app communication
URI associations can also be used to provide a simple form of inter-app communication. Two apps that register URI associations could send each other messages in their respective URI association parameters (typically apps use URI parameters to choose what page or information to display, but their usage is completely app dependent and could be used for messaging). The code example shows how this can be done with two apps sending text and displaying it in each other's UI. Note that the launching process will request to install the other app is installed if it has not been.
For this to work correctly it is necessary that communicating apps share a common protocol. A more generalised approach might be to define a "standard" protocol for messaging, or for querying other apps about what messaging protocols they support (ie send them a message with "tell me your protocols" and parse the response"). This would allow arbitrary communications with apps that support a set of protocols.
Document your custom URI protocol
Your application can now be started by, and can receive data from, other applications. But in order for this to work, you must document your protocol. You add the scheme to the URI Association Schemes List to advertise it to other developers.
Code sample
appcommunication.zip Code sample for section 'Inter-app communication'


Contents
Chintandave er - Thanks. Sub-edited and moved!
Hi, Thanks for this article.
I have sub-edited this article, added some category like Windows phone 8 and formatted code using code template. Also I changed the article title.
Have some suggestions for you.
You might want to check out wiki help articles Help:Formatting and Help:Wiki Article Review Checklist.
Regards
Chintan Dave.Chintandave er 13:25, 28 November 2012 (EET)
Yan -
Hi. WIth QR code, you can create a tiny url to your uri. For example with tyniurl.com,
yan_ 13:03, 29 November 2012 (EET)
Chintandave er - Question on register for an uri
Hi
You didn't explain in your article that what will happen if one phone has multiple apps with the same Uri name (like with myappname uri name) ? Which app phone will launch ?
Regards,
Chintan Dave.Chintandave er 07:23, 4 December 2012 (EET)
MaMi -
Hello,
I updated the article to answer your question
CheersMaMi 13:52, 4 December 2012 (EET)
Chintandave er - Thanks !
THanks for that. Nice.
Chintan Dave.Chintandave er 20:17, 5 December 2012 (EET)
Hamishwillee - Review/feedback
Hi Mami
OK, so first I think this is an well written and relatively comprehensive article. The only thing that is I can see missing is there is no explanation of how to get the passed parameters when you get the the MainPage (or wherever the message was mapped to). I would probably also add a comment early in introduction that you can't use any URI you like because some are reserved (and link to that list).
I particularly like that you created the section "What will and will not work", and that you've added links to the references. Great job.
My only concern with this is that the original documents are pretty comprehensive too. Generally we don't create new articles if there are already good documents that do the job on DevCenter - we recommend "adding value". To some extent you do this by having a code example, and with your real world testing. However I'm not sure that the judges will see this as sufficiently valuable. You might want to consider if there are other "innovative" ways that you could use the material you've learned here in an entry - as an example, something like Link2SMS - a protocol handling example (and I'm not saying this is better than what you did, just that it explores a use case that isn't documented elsewhere).
Whatever happens with the competition, I like your writing approach and I hope to see more of you on the wiki.
Regards
Hamishhamishwillee 07:13, 7 December 2012 (EET)
MaMi -
copy that, I agree with your point
at first I just wanted to share the results of my investigations but thought it would be too light, moreover it is just a small part of the original msdn content and I thought it could worth it to have this specific part on a dedicated article.
also there already is a link to reserved URI list at the end of the article.
regardsMaMi 16:51, 7 December 2012 (EET)
Hamishwillee - Indeed
Thanks!
Can you please add the " explanation of how to get the passed parameters when you get the the MainPage"? At least then you have the main points all covered.
Regards
Hamishhamishwillee 04:26, 8 December 2012 (EET)
MaMi -
sure, doneMaMi 12:55, 10 December 2012 (EET)
Hamishwillee - Thanks.
I don't know whether or not this article will win in the competition, but I am more than pleased to have it here, particularly because of the "what will and will not work" section (this is what it "value adds", along with the example). Thank youhamishwillee 06:57, 19 December 2012 (EET)
Vantech - Email Uri Link
Hi, I am having trouble opening the App from a Url in a Email. What do I put in the Email to make it open the App? Can you please Help me? Thanks,
Vandan Patelvantech 04:52, 1 January 2013 (EET)
Influencer - Another sample?
I created another sample consisting of two apps because I wanted to see how that works. One calls the other and sends some text. Someone interested in it, shall I upload it here?
Thomasinfluencer 00:42, 31 May 2013 (EEST)
Hamishwillee - Depends
Hi Thomas
Well presumably you just use mailto URL in your link - so does the test code show anything special? On the face of it I'm not sure how such could fit in this article
Thanks very much for your updates to #Document_your_custom_URI_protocol. Very timely as I'm about the have a campaign to get more people to document their protocols . I will probably pull that section into its own document tomorrow to make promotion easier.
Regards
Hamishhamishwillee 09:34, 3 June 2013 (EEST)
Influencer - My motivation
Well,
I thought it might be more useful to show that two apps can call one another instead of an app calling itself. App2 shows a text and registers for URI assocs. App1 launches app2 and sends its own text, that is then displayed in app2. The launching process and the request to install the other app can be seen. I don't use a mailto URL but a self-registered URI as in the sample above. My Point is that the launch of another app is shown.
Regarding the URI protocol docs campaign, that's a very good idea. I think we should have at least sections for OS-defined URIs, Nokia-defined URIs and App-defined URIs. I hope that will give us new opportunities for interesting app-cooperations.
I'm currently encouraging a colleague of mine to insert this feature into his newspapers-app. Th problem is it works only on WP8 although the necessary mechanism is there in WP7 but the manifest change is not recognized. So he has to pushlish a special WP8 version first.
Thomasinfluencer 10:56, 3 June 2013 (EEST)
Hamishwillee - Thanks Thomas
Hi Thomas
Well yes, the whole purpose of the URL associations is to provide a mechanism for inter-app communication. Most of the communication is one way - "start in this cool mode", but obviously you can use it to get back information too as Nokia music allows.
So yes, it is a good idea to show how an app can call this in this article (even though it is covered in places like Nokia Music app-to-app protocol for Windows Phone 8). Can you take care of that?
Yes, we should have all those sections. Usually we will link to the manufacturer page to make it clear that its a "supported API". We would also have a "hacked" section to show know URIs where these have been extracted from a XAP (you can get "launcher-style" URIs from the Xap inspection) as these are not necessarily supported by the developer.
Great you are encouraging the developer to insert this feature. I am going to start by asking the champions if they have any links or if they know any. We will also be asking the "big boys" - skype, foursquare, twitter etc through the appropriate program management.
A job for last week that i hope to do this week :-0
Thanks again, its easy to see a hole in our offering, but a lot less people actually bother to fill it.
Regards
Hhamishwillee 11:30, 3 June 2013 (EEST)
Influencer - Improving my sample...
Hell, yes, I could add a reply feature to my sample...influencer 11:56, 3 June 2013 (EEST)
Influencer - Added the sample
I added my sample and a new section called 'Two-way communication '.
Please teview and give comments.
Thomasinfluencer 00:13, 12 June 2013 (EEST)
Influencer - ps
PS: I don't see from the mentioned article how info is getting back...influencer 00:25, 12 June 2013 (EEST)
Hamishwillee - Reviewed
Hi Thomas
Thanks very much - nice job. I've just subedited it to say what I want :-) Hope you think it is an improvement
I think you are asking how you might request information using a URI? If so, this is a launcher/one direction only API. Hence the only way to make this like a "chooser" is to define a protocol which allows two way communication back.
I think that that would make a super-cool blog. "How to write your own URI association chooser". So app defines URI for launching itself and some parameters - including the calling app name (if that is not included in the startup information, haven't checked) and what information it wants. Then sends back the information in a reverse message. The whole thing could be quite pretty, and I like the idea of such loose coupling.
Regards
Hhamishwillee 10:10, 12 June 2013 (EEST)