You are on page 1of 31

M10: App to App Communication

Andy Wigley | Microsoft Technical Evangelist Rob Tiffany | Microsoft Enterprise Mobility Strategist

This App

async

This await App

Target Agenda | Day 1


Module and Topic | 10-minute breaks after each session / 60-minute meal break 1a - Introducing Windows Phone 8 Application Development | Part 1 1b - Introducing Windows Phone 8 Application Development | Part 2 2 - Designing Windows Phone Apps Planned Duration 50:00 50:00 50:00

3 - Building Windows Phone Apps


4 - Files and Storage on Windows Phone 8 Meal Break | 60-minutes 5 - Windows Phone 8 Application Lifecycle 6 - Background Agents 7 - Tiles and Lock Screen Notifications 8 - Push Notifications

50:00
50:00 60:00 50:00 25:00 25:00 30:00

9 - Using Phone Resources on Windows Phone 8

50:00

Target Agenda | Day 2


Module and Topic | 10-minute breaks after each session / 60-minute meal break 10 - App to App Communication 11 - Network Communication on Windows Phone 8 12 - Proximity Sensors and Bluetooth Planned Duration 35:00 50:00 35:00

13 - Speech Input on Windows Phone 8


14 - Maps and Location on Windows Phone 8 15 - Wallet Support 16 - In App Purchasing Meal Break | 60-minutes 17 - The Windows Phone Store 18 - Enterprise Applications in Windows Phone 8: Architecture and Publishing

35:00
35:00 25:00 25:00 60:00 50:00 50:00

19 - Windows 8 and Windows Phone 8 Cross Platform Development


20 Mobile Web

50:00
50:00

Module Agenda

Auto-Launching with File and Protocol Associations Launching Apps to Handle Particular File Types Launching one App from Another

Auto-Launching with File and Protocol Associations

12/4/2012

Auto-launching with File and Protocol Associations


File associations allow your app to launch when the user wants to open a particular file

type, via:
an email attachment a website via Internet Explorer a text message a Near Field Communications (NFC) tag another app from the Store Protocol association allows your app to automatically launch when another app launches a special URI Protocol is the first part of a URI, e.g. myprotocol:/ShowProducts?CategoryID=aea6ae1f Your app launches another and passes it data in the remainder of the launch URI

User Experience with File and Protocol Associations


When a user launches a file or protocol from an app

If there is only one app on the phone registered for that file or
protocol, the app is automatically launched If there is more than one app registered for that file or protocol, the user is asked which app they want to use If no apps on the phone can handle that file or protocol, the user is given the option to get one that does

Comparison with Windows 8 User Experience


Like Windows 8, Windows Phone 8 uses

Launcher.LaunchFileAsync(IStorageFile) to launch a file and


Launcher.LaunchUriAsync(Uri) to launch a URI However, the way Windows Phone XAML apps receive a file or

URI is different
Windows 8 has a default Store app for a file type or URI, so that will be launched In Windows Phone 8, if there are multiple Store apps installed that can handle a particular file or protocol association, the user chooses the receiving app from a menu

Demo 1: User Experience

File Associations

Registering for a File Association


To handle a particular file type, register for a file association in the app manifest file

Optionally supply logos that Windows Phone OS will use when listing files
Logo Size
Small Medium Large

Use
Email attachments Office hub list view Browser download

Dimensions
33x33 pixels 69x69 pixels 176x176 pixels

Edit WMAppManifest.xml using the XML (Text) Editor

Adding a File Association to WMAppManifest.xml


Add a FileTypeAssociation element inside the Extensions element The Extensions element must follow immediately after the Tokens element Specify up to 20 file extensions per file type association
<Extensions> <FileTypeAssociation Name="BugQuery" TaskID="_default" NavUriFragment="fileToken=%s"> <Logos> <Logo Size="small">bug-small-33x33.png</Logo> <Logo Size="medium">bug-medium-69x69.png</Logo> <Logo Size="large">bug-large-176x176.png</Logo> </Logos> <SupportedFileTypes> <FileType>.bqy</FileType> </SupportedFileTypes> </FileTypeAssociation> </Extensions>

Listening for a file launch


When your app is launched to handle a file, a deep link URI is sent to your app
/FileTypeAssociation?fileToken=89819279-4fe0-4531-9f57-d633f0949a19

FileTypeAssociation designates that the source of the URI is a file type association

The file token

You need to implement a custom URI Mapper to parse the deep link URI and map to a page in your app that will handle it

Custom URI Mapper - 1


using System; using System.Windows.Navigation; using Windows.Phone.Storage.SharedAccess; namespace FileAssociationsHandler { class AssociationUriMapper : UriMapperBase { private string tempUri; public override Uri MapUri(Uri uri) { tempUri = uri.ToString(); // File association launch if (tempUri.Contains("/FileTypeAssociation")) { // Get the file ID (after "fileToken="). int fileIDIndex = tempUri.IndexOf("fileToken=") + 10; string fileID = tempUri.Substring(fileIDIndex); // Get the file name. string incomingFileName = SharedStorageAccessManager.GetSharedFileName(fileID); ...

Custom URI Mapper - 2


...
// Get the file extension. int extensionIndex = incomingFileName.LastIndexOf('.') + 1; string incomingFileType = incomingFileName.Substring(extensionIndex).ToLower(); // Map the .bqy and .bdp files to the appropriate pages. switch (incomingFileType) { case "bqy": return new Uri("/BugQueryPage.xaml?fileToken=" + fileID, UriKind.Relative); case "bdp": return new Uri("/BugDetailPage.xaml?fileToken=" + fileID, UriKind.Relative); default: return new Uri("/MainPage.xaml", UriKind.Relative); } } // Map everything else to the main page. return new Uri("/MainPage.xaml", UriKind.Relative); } }

Using the URI Mapper


Assign the custom URI Mapper to the root frame of the app in App.xaml.cs
private void InitializePhoneApplication() { if (phoneApplicationInitialized) return; // Create the frame but don't set it as RootVisual yet; this allows the splash // screen to remain active until the application is ready to render. RootFrame = new PhoneApplicationFrame(); RootFrame.Navigated += CompleteInitializePhoneApplication; // Assign the URI-mapper class to the application frame. RootFrame.UriMapper = new AssociationUriMapper(); // Handle navigation failures RootFrame.NavigationFailed += RootFrame_NavigationFailed; // Handle reset requests for clearing the backstack RootFrame.Navigated += CheckForResetNavigation; // Ensure we don't initialize again phoneApplicationInitialized = true; }

Accessing the File


Files passed to an app are stored by the OS in

a special folder called SharedStorage


Receiving apps only have read access to this folder Copy file to local storage to access it

SharedStorage

Local Storage

Retrieving the File


Use the SharedStorageAccessManager.GetSharedFileName and SharedStorageAccessManager.CopySharedFileAsync methods to access the file
protected async override void OnNavigatedTo(NavigationEventArgs e) { // Get a dictionary of URI parameters and values. IDictionary<string, string> queryStrings = this.NavigationContext.QueryString; // Have we been launched to handle a file association? if (queryStrings.ContainsKey("fileToken")) { // Yes we have - get the file token string fileToken = queryStrings["fileToken"]; // Copy the file from shared storage string filename = SharedStorageAccessManager.GetSharedFileName(fileToken); IStorageFile bugQueryFile = await SharedStorageAccessManager.CopySharedFileAsync( ApplicationData.Current.LocalFolder, // Store in the local folder filename, // keep the same filename NameCollisionOption.ReplaceExisting, // Replace any existing file of the same name fileToken); // Do something with the file... } ... }

Sending a File to Another App


Your app can launch a file so another app can open it
private async void LaunchFileButton_Click(object sender, RoutedEventArgs rea) { // Access local storage. StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder; // Access the bug query file. StorageFile bqfile = await local.GetFileAsync("file1.bqy"); // Launch the bug query file. Windows.System.Launcher.LaunchFileAsync(bqfile); }

Reserved File Associations


Many file extensions are reserved for the built-in apps

.cer, .doc, .docx, .jpg, .mp3, .pptx etc..


Many more reserved by the OS .ade, .adp .[ > 100 in total! ] .xnk

If you try to reserve a file association using one of the reserved types, the reservation
request will be ignored See the documentation for a full list of the reserved file types

20

12/4/2012

Demo 2: File Associations

Protocol Associations

Protocol Associations
Protocol association allows your app to automatically launch when another app launches a

special URI
The URI begins with a protocol name that your app has registered for For example, contoso is the protocol name in the following URI:

contoso:ShowProducts?CategoryID=aea6ae1f-9894-404e-8bca-ec47ec5b9c6c
After the colon, the rest of the URI can be set to whatever you want

Adding a Protocol Association to WMAppManifest.xml


To register your app for a protocol association, add a Protocol element inside the

Extensions element
The Extensions element must follow immediately after the Tokens element Maximum of 10 protocol associations per app
<Extensions> <Protocol Name= "contoso" TaskID="_default" NavUriFragment="encodedLaunchUri=%s"> </Extensions>

Listening for a URI


When your app is launched to handle a protocol association, a deep link URI is sent to

your app
/Protocol?encodedLaunchUri=contoso:ShowProducts?CategoryID=aea6ae1f Protocol designates that the source of the URI is a protocol association The full encoded launch URI

Implement a custom URI Mapper to parse the deep link URI and map to a page in your

app that will handle it, same as for File Associations

Launching a URI
Use the LaunchUriAsync method to launch another app that is registered for that protocol
private void Button_Click_1(object sender, RoutedEventArgs e) { // Launch a protocol Windows.System.Launcher.LaunchUriAsync(new Uri("jumpstart:NewSession")); }

Reserved Protocol Associations


Some protocols are reserved for the built-in apps

http:, MailTo:, Map:


Many more reserved by the OS File:, Iehistory:, Javascript:, many more

If you try to reserve a protocol association using one of the reserved protocols, the
reservation request will be ignored See the documentation for a full list of the reserved protocols

Launching Built-in Apps


Use LaunchUriAsync to launch many of the built-in apps

URI scheme
http:[URL] mailto:[email address]
ms-settings-accounts: ms-settings-airplanemode:

Description
Launches the web browser and navigates to the specified URL.
Launches the email app and creates a new message with the specified email address on the To line. Note that the email is not sent until the user taps send. Launches the Account Settings app. Launches the Airplane Mode Settings app.

ms-settings-bluetooth:
ms-settings-cellular: ms-settings-emailandaccounts: ms-settings-location:

Launches the Bluetooth Settings app.


Launches the Cellular Settings app. Launches the email and accounts settings app. Launches the Location Settings app.

ms-settings-lock:
ms-settings-wifi:

Launches the Lock Screen settings app.


Launches the Wi-Fi Settings app.

Launching Built-in Apps (continued)


URI scheme
zune:navigate?appid=[app ID] zune:reviewapp zune:reviewapp?appid=[app ID] zune:search?[search parameter]=[value] zune:search?keyword=[search keyword] &contenttype=app zune:search?publisher=[publisher name] zune:navigate?appid=[app ID]

Description
Launches the Windows Phone Store and shows the details page for the specified app. Launches the Store and shows the review page for the calling app. Launches the Store and shows the review page for the specified app. Launches the Store and searches for the specified content. Launches the Store and searches for apps by keyword. Launches the Store and searches for items by publisher name. Launches the Windows Phone Store and shows the details page for the specified app. Launches the Store and shows the review page for the calling app. Launches the Store and shows the review page for the specified app. Launches the Store and searches for the specified content.

zune:reviewapp zune:reviewapp?appid=[app ID] zune:search?[search parameter]=[value]

Summary
App can register to handle particular file types

When the user opens a file from a website, email message or SMS message, your app is
launched to process the file One app can launch another by launching a file of a type for which the second app has

registered a file association


App can register for an association with particular URI protocols One app can launch another by launching a URI using a protocol for which the second app

has registered a protocol association


If more than one app has registered the same file or protocol association, the user is asked to select which app should be launched

The information herein is for informational purposes only an represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be

interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation.

MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.

You might also like