Windows Phone 8 - 11 App to App Communication
Upcoming SlideShare
Loading in...5
×
 

Windows Phone 8 - 11 App to App Communication

on

  • 1,034 views

 

Statistics

Views

Total Views
1,034
Views on SlideShare
1,028
Embed Views
6

Actions

Likes
0
Downloads
10
Comments
0

1 Embed 6

http://www.slideee.com 6

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • This session is about File and Protocol Associations.It is perhaps erroneously referred to as ‘App to App’ communication, which although being one of the interesting things you can do with it, is not the *only* thing.It’s about some interesting ways you can launch your app to handle specific files that are being sent to it, or because an app or the shell has ‘opened’ a particular custom URI that your app is registered to handle.
  • You can use file and URI associations in Windows Phone 8 to automatically launch your app when another app launches a specific file type or URI scheme. When launched, a deep link URI is used to send the file (a reference to the file) or URI to your app. You can also use the association launching API to launch another app in the same way.File associations allow your app to automatically launch when the user wants to open a particular file. That file could come from a variety of sources including, but not limited to: an email attachmenta website via Internet Explorera Near Field Communications (NFC) taganother app from the StoreA URI association allows your app to automatically launch when another app launches a special URI. What makes that URI special is that it begins with a URI scheme name that your app has registered for. The URI scheme name is the portion of the URI that comes just before the colon (‘:’).
  • How association works for the user When the user launches a file or URI from an app, what happens next depends on which apps are installed on the phone. If no apps on the phone can handle that particular file or protocol association, the user will be given the option to get one that does.When there is only one app on the phone registered for a particular file or protocol association, that app will be automatically launched when the user wants to open it. If a user has more than one app on their phone registered for a file or protocol association, each time the user opens the file, they will be as asked which app they want to use.Tip: Don’t register for a file or protocol association unless you can perform a useful action with it. If your app causes the app menu to be shown frequently, the user may choose to uninstall it altogether to avoid having to always make a choice.
  • 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. Also, Windows Phone does not have a “default” Store app. If there are multiple Store apps installed that can handle a particular file or URI association, the user chooses the receiving app from a menu.
  • For demonstration notes please look in the “Demonstration” folder and open the corresponding Word document.
  • To handle a particular file type, register for a file association in the app manifest file. Your app can specify any file association that it is able to handle. However, any file associations that are reserved by built-in apps will be ignored (we’ll look at what those are a little later).To provide an “integrated” look to custom file types appearing in built-in apps, you have the option of supplying logos (image files) to appear beside the files. For example, if a file (of your custom type) were attached to an email message, a small logo would appear beside the attachment after it was downloaded. The table on the slide lists the three image sizes that you can provide for each file association.
  • You can specify a number of elements in a FileTypeAssociation:Extensions - Must follow the Tokens element.FileTypeAssociation -Describes a file association. You can register a maximum of 20 file associations. The Name attribute is required, but you choose your own friendly name. Specify the TaskID and NavUriFragment elements as shown.Logos - Lists all logos for a file association. This is optional when no logos are specified. You must provide all sizes (small, medium, large) if you provide any.SupportedFileTypes - Lists all file extensions that are associated with the file type.FileType - Lists the file extension that is associated with the file type, including the period (‘.’). You can associate a maximum of 20 file extensions with each file association.
  • When your app is launched to handle a particular file type, a deep link URI is used to take the user to your app. Within the URI, the FileTypeAssociation string designates that the source of the URI is a file association and the fileToken parameter contains the file token. Upon launch, map the incoming deep link URI to an app page that can handle the file. Your app will launch at the standard launch page, but if you have multiple pages to handle multiple file types, create a custom URI mapper and in it call the GetSharedFileName method to check the file type before mapping the URI.
  • Here’s an example of a custom URI Mapper.This first checks that the deep link Uri is from a File Type Association. It then extracts the FileID which has been supplied in the deeplink URI query string, which you can pass to the SharedStorageAccessManager.GetSharedFileName() method to get the filename of the fie you are being sent.
  • Then based on the file extension, we route the request to a particular page.
  • This is how you wire in your custom URI Mapper.When the app is launched, it assigns the URI mapper during initialization. Before launching any pages, the app calls the MapUri method of the URI mapper to determine which page to launch. The URI that the URI mapper returns is the page that the app launches.
  • Don’t be mistaken into thinking that the SharedStorage area is some kind of shared folder to which you have complete read/write access.No, it’s just a temporary storage area used by the OS where it stores the file while it attempts to launch your app.[Click to play animation]The file comes in either by email, or from a webpage, or from another app and is copied into SharedStorage.Your app is then launched and passed a token to that file, and all it can do is to copy that file from SharedStorage into its local folder, whereupon it can open the file and process it.
  • In your app, your UriMapper has mapped the File Association launch into a navigation to a particular page.On the target page, your OnNavigatedTo method executes as normal, and you can extract the data from the query string (you must be sure that your UriMapper passes the File Token in the query string it sends to the launched page).Then you can use the SharedStorageAccessManager.GetSharedFileName(fileToken) to get the name of the file, and the SharedStorageAccessManager.CopySharedFileAsync(…) method to copy the file from SharedStorage into the local folder.
  • As mentioned earlier, your app also can launch a file, so another app can open it. To do this, use the LauncherLaunchFileAsync(IStorageFile) method from the Launcher object of the Windows.System namespace. For example, the code shown launches a fictitious Contoso bug query file from local storage.
  • There are over 100 reserved file extensions, including all the usual suspects.See the docs for full details.
  • For demonstration notes please look in the “Demonstration” folder and open the corresponding Word document.
  • A URI association allows your app to automatically launch when another app launches a special URI. What makes that URI special is that it begins with a URI scheme name that your app has registered for. The URI scheme name is the portion of the URI that comes just before the colon (‘:’). The URI scheme includes the URI scheme name and all of the URI that follows the colon. For example, in the URI scheme shown, contoso is the URI scheme name.After the colon, the rest of the URI is open to however you want to use the URI association. When your app is launched, it has the opportunity to inspect the URI and behave differently based on what is in the URI. In this example, the receiving app has been designed to show all products within a particular category.
  • To register your app for a protocol association, add a Protocol element inside the Extensions element.Extensions - Must follow the Tokens element.FileTypeAssociation -Describes a file association. You can register a maximum of 20 file associations. The Name attribute is required, but you choose your own friendly name. Specify the TaskID and NavUriFragment elements as shown.Logos - Lists all logos for a file association. This is optional when no logos are specified. You must provide all sizes (small, medium, large) if you provide any.SupportedFileTypes - Lists all file extensions that are associated with the file type.FileType - Lists the file extension that is associated with the file type, including the period (‘.’). You can associate a maximum of 20 file extensions with each file association.
  • The process of handling a launch for a protocol association is exactly the same as with File associations. In this case, the deep link URI starts with /Protocol?encodedLaunchUri, so create a UriMapper that looks for that and maps the launch to the appropriate page in your application.
  • One app can use this to launch another app. To do this, use the LauncherUriAsync(Uri) method from the Launcher object of the Windows.System namespace.
  • As with file associations, certain URIs are reserved for system use. If you try to reserve one of tese, your registration request will be ignored.Again, see the docs for full details.
  • As already mentioned in the Accessing Phone Resources module, the LaunchUriAsync method is also used to launch several built-in applications. This usage is shown here in this table, and as you can see is mainly used for launching settings dialogs, but may also be used to launch the browser or the built-in New email application.
  • Certain of the built-in URIs map to the Windows Phone Store. This is useful for adding links onto the ‘About’ page of your app to encourage users to add feedback (positive hopefully!) or to lead them to other apps you have published.
  • For demonstration notes please look in the “Demonstration” folder and open the corresponding Word document.

Windows Phone 8 - 11 App to App Communication Windows Phone 8 - 11 App to App Communication Presentation Transcript

  • Oliver Scheer Senior Technical Evangelist Microsoft Deutschland http://the-oliver.com File and Protocol Associations This App   This App
  • Agenda 3/18/2014Microsoft confidential2 Launching Apps to Handle Particular File Types Launching one App from Another Auto-Launching with File and Protocol Associations Automatically launch your app when another app launches a particular file type or protocol File associations allow your app to launch to handle an email attachment, a file opened in Internet Explorer or by another app
  • • 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 Auto-launching with File and Protocol Associations 3/18/2014Microsoft confidential3
  • • 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 User Experience with File and Protocol Associations 3/18/2014Microsoft confidential4
  • • 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 Comparison with Windows 8 User Experience 3/18/2014Microsoft confidential5
  • File Associations 3/18/2014Microsoft confidential7
  • • 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 • Edit WMAppManifest.xml using the XML (Text) Editor Registering for a File Association 3/18/2014Microsoft confidential8 Logo Size Use Dimensions Small Email attachments 33x33 pixels Medium Office hub list view 69x69 pixels Large Browser download 176x176 pixels
  • • 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> Adding a File Association to WMAppManifest.xml 3/18/2014Microsoft confidential9
  • • 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 • 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 Listening for a file launch 3/18/2014Microsoft confidential10 FileTypeAssociation designates that the source of the URI is a file type association The file token
  • 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 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; } • Assign the custom URI Mapper to the root frame of the app in App.xaml.cs
  • Local Storage SharedStorage • 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 Accessing the File 3/18/201414
  • Retrieving 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... } ... } • Use the SharedStorageAccessManager.GetSharedFileName and SharedStorageAccessManager.CopySharedFileAsync methods to access the file
  • • Your app can launch a file so another app can open it Sending a File to Another App 3/18/201416 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); }
  • • 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 Reserved File Associations 3/18/2014Microsoft confidential17
  • Protocol Associations 3/18/2014Microsoft confidential19
  • • 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 Protocol Associations 3/18/2014Microsoft confidential20
  • • 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> Adding a Protocol Association to WMAppManifest.xml 3/18/2014Microsoft confidential21
  • • 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 • 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 Listening for a URI 3/18/2014Microsoft confidential22 Protocol designates that the source of the URI is a protocol association The full encoded launch URI
  • • Use the LaunchUriAsync method to launch another app that is registered for that protocol Launching a URI 3/18/201423 private void Button_Click_1(object sender, RoutedEventArgs e) { // Launch a protocol Windows.System.Launcher.LaunchUriAsync(new Uri("jumpstart:NewSession")); }
  • • 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 Reserved Protocol Associations 3/18/2014Microsoft confidential24
  • URI scheme Description http:[URL] Launches the web browser and navigates to the specified URL. mailto:[email address] 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. ms-settings-accounts: Launches the Account Settings app. ms-settings-airplanemode: Launches the Airplane Mode Settings app. ms-settings-bluetooth: Launches the Bluetooth Settings app. ms-settings-cellular: Launches the Cellular Settings app. ms-settings-emailandaccounts: Launches the email and accounts settings app. ms-settings-location: Launches the Location Settings app. ms-settings-lock: Launches the Lock Screen settings app. ms-settings-wifi: Launches the Wi-Fi Settings app. Launching Built-in Apps Use LaunchUriAsync to launch many of the built-in apps 3/18/2014Microsoft confidential25
  • Launching Built-in Apps (cont) 3/18/2014Microsoft confidential26 URI scheme Description zune:navigate?appid=[app ID] Launches the Windows Phone Store and shows the details page for the specified app. zune:reviewapp Launches the Store and shows the review page for the calling app. zune:reviewapp?appid=[app ID] Launches the Store and shows the review page for the specified app. zune:search?[search parameter]=[value] Launches the Store and searches for the specified content. zune:search?keyword=[search keyword] &contenttype=app Launches the Store and searches for apps by keyword. zune:search?publisher=[publisher name] Launches the Store and searches for items by publisher name.
  • • 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 Summary 3/18/2014
  • 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. © 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. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.