Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

(Difference between revisions)

URI associations for Windows Phone

From Wiki
Jump to: navigation, search
yan_ (Talk | contribs)
(Yan -)
hamishwillee (Talk | contribs)
m (Hamishwillee - Bot update - Fix metadata)
Line 3: Line 3:
 
{{Note|This is a community entry in the [[Windows Phone 8 Wiki Competition 2012Q4]].}}
 
{{Note|This is a community entry in the [[Windows Phone 8 Wiki Competition 2012Q4]].}}
 
{{ArticleMetaData <!-- v1.2 -->
 
{{ArticleMetaData <!-- v1.2 -->
|sourcecode= [[Media:UriAssociation.zip]]
+
|sourcecode= [[Media:UriAssociation.zip]]
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 
|devices= <!-- Devices tested against - e.g. ''devices=Nokia 6131 NFC, Nokia C7-00'') -->
 
|devices= <!-- Devices tested against - e.g. ''devices=Nokia 6131 NFC, Nokia C7-00'') -->
Line 9: Line 9:
 
|platform= Windows Phone 8
 
|platform= Windows Phone 8
 
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
 
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->  
+
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
|signing=<!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
+
|signing= <!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
 
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 
|keywords= Custom URI
 
|keywords= Custom URI
 
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 
|translated-by= <!-- [[User:XXXX]] -->
 
|translated-by= <!-- [[User:XXXX]] -->
|translated-from-title= <!-- Title only -->  
+
|translated-from-title= <!-- Title only -->
 
|translated-from-id= <!-- Id of translated revision -->
 
|translated-from-id= <!-- Id of translated revision -->
|review-by=<!-- After re-review: [[User:username]] -->
+
|review-by= <!-- After re-review: [[User:username]] -->
 
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 
|update-by= <!-- After significant update: [[User:username]]-->
 
|update-by= <!-- After significant update: [[User:username]]-->
Line 107: Line 107:
  
 
[http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj207065(v=vs.105).aspx Reserved files and uri associations]
 
[http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj207065(v=vs.105).aspx Reserved files and uri associations]
 +
[[Category:Code Examples]]

Revision as of 04:17, 29 November 2012

This article explains how to associate your application with an uri format in order to launch it when such uri is detected

Note.pngNote: This is a community entry in the Windows Phone 8 Wiki Competition 2012Q4.

WP Metro Icon File.png
WP Metro Icon NFC.png
SignpostIcon XAML 40.png
WP Metro Icon WP8.png
Article Metadata
Code ExampleCompatibility
Platform(s): Windows Phone 8
Windows Phone 8
Article
Keywords: Custom URI
Created: MaMi (28 Nov 2012)
Last edited: hamishwillee (29 Nov 2012)

Contents

Introduction

Windows phone 8 introduced the possibility for your app to register to a predifined kind of uri. This will allow you to launch your application from various source and with various parameters. The uri will have to be formatted as follow

"<Custom Protocol Name>" : "<The landing page>""<Some optional parameters if needed>"

For example:

myappuri:MainPage?Category=5

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:

<Extensions>
<Protocol Name="myappuri" NavUriFragment="encodedLaunchUri=%s" TaskID="_default" />
</Extensions>

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
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 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

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

Code sample

Media:UriAssociation.zip

Reference

Official documentation about uri associations on MSDN

Reserved files and uri associations

897 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×