Mobile Zone is brought to you in partnership with:

Jerry Nixon is the Microsoft Developer Evangelist in Colorado. He has scary knowledge about Architecture, XAML, and SQL Server. He has terrifying knowledge about Windows Phone, the Kinect, and C#. And, he has simply ridiculous knowledge on Windows Desktop development in Windows 8. Jerry is a DZone MVB and is not an employee of DZone and has posted 104 posts at DZone. You can read more from them at their website. View Full User Profile

Walkthrough: Using Windows 8 Custom Protocol Activation

10.25.2012
| 4905 views |
  • submit to reddit
Protocol activation, what is it? Well, when you run “http:{something}” you expect a browser to launch, right? When you type “mailto:{something}” you expect a mail client to launch, right? What if you could create “custom:{something}” that launched your app? That’s custom protocol activation!

MSDN: Windows allows an app to register to become the default handler for a certain URI scheme name. Both desktop and Windows Store apps can register to be a default handler for a URI scheme name. If the user chooses your app as the default handler for a URI scheme name, your app will be activated every time that type of URI is launched. You should only register for a URI scheme name if you expect to handle all URI launches for that type of URI scheme. If you do choose to register for a URI scheme name it is important that you provide the end user with the functionality that is expected when your app is activated for that URI scheme. For example an app that registers for the mailto: URI scheme name should open to a new e-mail message so that the user can compose a new e-mail. For more info on file associations see Guidelines and checklist for file types and URIs.

Here’s a quick protocol demo

Not just any ole protocol

MSDNHere is an alphabetic list of URI scheme names that you cannot enter into the package manifest because they are either reserved or forbidden: Application.manifest, Application.reference, Batfile, BLOB, Cerfile,Chm.file, Cmdfile, Comfile, Cplfile, Dllfile, Drvfile, Exefile, Explorer.AssocActionId. BurnSelection,Explorer. AssocActionId.CloseSession, Explorer.AssocActionId. EraseDisc, Explorer.AssocActionId. ZipSelection,Explorer. AssocProtocol.search-ms, Explorer.BurnSelection, Explorer.CloseSession, Explorer.EraseDisc, Explorer.ZipSelection, FILE, Fonfile, Hlpfile, Htafile, Inffile , Insfile, InternetShortcut, Jsefile, Lnkfile,Microsoft.PowerShellScript.1, MS-accountpictureprovider, Ms-appdata, Ms-appx, MS-AUTOPLAY, Msi.Package,Msi.Patch, Ms-windows-store, Ocxfile, Piffile, Regfile, Scrfile, Scriptletfile, Shbfile, Shcmdfile, Shsfile, SMB,Sysfile, Ttffile, Unknown, userTileProvider,vbefile, Vbsfile, Windows.gadget, Wsffile, Wsfile, Wshfile.

Some find things to try

The Bing Maps app that comes installed on Windows 8 supports protocol activation. This means you can create shortcuts into it. It also means you can leverage it from your application. In fact many apps that include the Bing Maps control, really shouldn’t. Try running this:

bingmaps:?q=coffee&where=Omaha

Application Manifest

MSDN: The file type and protocol association model has changed in Windows 8. Apps are no longer able to programmatically set themselves as the default handler for a file type or protocol. Instead, now the user always controls what the default handler is for a file type or protocol.

image

That screen above is the application manifest. It’s on the declarations tab where we can add the “protocol” declaration.

MSDN: Your app can use existing protocols for communication, such as mailto, or create a custom protocol. The protocol activation extension enables you to define a custom protocol or register to handle an existing protocol.

Let’s take it to another level!

In the video above, one key operation was for my application to call a Uri. To do this in C#,  we use LaunchUriAsync and pass in the Uri we want to launch. Like this:

image

Security considerations

MSDN: As noted above, the URL that is passed to an application protocol handler might be broken across multiple parameters. Malicious parties could use additional quotation marks or backslash characters to pass additional command-line parameters. For this reason, application protocol handlers should assume that any parameters on the command line could come from malicious parties, and carefully validate them. Applications that could initiate dangerous actions based on external data must first confirm those actions with the user. In addition, handling applications should be tested with URLs that are overly long or contain unexpected (or undesirable) character sequences.

Conclusion

Why not? Protocol activation is just another cool way to activate your app. It’s easy (I hope that’s obvious) and doesn’t cost you a thing. And, maybe, just maybe, you can do even more than you originally dreamed with your app – engage imagination.

Best of luck!

 

 

 

Published at DZone with permission of Jerry Nixon, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)