{"id":25850,"date":"2016-05-18T13:25:36","date_gmt":"2016-05-18T20:25:36","guid":{"rendered":"https:\/\/blog.xamarin.com\/?p=25850"},"modified":"2019-04-04T09:05:08","modified_gmt":"2019-04-04T16:05:08","slug":"deep-link-content-with-xamarin-forms-url-navigation","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/deep-link-content-with-xamarin-forms-url-navigation\/","title":{"rendered":"Deep Link Content with Xamarin.Forms URL Navigation"},"content":{"rendered":"<p>\t\t\t\tLeveraging mobile search ensures your mobile app stands out on your users&#8217; devices. In addition to the powerful search built into each device, deep linking to your app from web content creates a fully connected experience to keep your users in your app and coming back for more. URL Navigation simplifies this entire process with just a few simple methods.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-25852\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/DeepLinkFlow.png\" alt=\"DeepLinkFlow\" width=\"860\" height=\"438\" \/><\/p>\n<p>The Xamarin Evolve conference application (source code available on <a href=\"http:\/\/github.com\/xamarinhq\/app-evolve\">GitHub<\/a>) utilized the new URL Navigation to deep link all of the sessions of the conference. If you search for a session in Google Search, Spotlight Search, or on the web, the app automatically launches the session details page!<\/p>\n<p>In this post, we&#8217;ll take a look at exactly how this was implemented for both iOS and Android.<\/p>\n<h2>Getting Started with URL Navigation<\/h2>\n<p>To try out URL Navigation, you&#8217;ll have to update your Xamarin.Forms NuGet Packages to the 2.3.X pre-release packages. <\/p>\n<h3>Android<\/h3>\n<p>For Android, you&#8217;ll also need to install the <a href=\"https:\/\/www.nuget.org\/packages\/Xamarin.Forms.AppLinks\/2.3.0.38-pre2\">Xamarin.Forms AppLinks NuGet<\/a> in your Android project.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-25851\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/AppLinksNuGet.png\" alt=\"AppLinksNuGet\" width=\"1502\" height=\"368\" \/><\/p>\n<p>Next, in the MainActivity underneath the Forms.Init(this); method call, add the following:<\/p>\n<pre class=\"lang:csharp decode:true\">\r\nAndroidAppLinks.Init(this);\r\n<\/pre>\n<p>To get a full handle on using Google Search and App Indexing, be sure to <a href=\"https:\/\/blog.xamarin.com\/connect-with-your-users-with-google-search-and-app-indexing\/\">read my blog post<\/a> and follow the same setup in the Search Console and Intent Filters on your MainActivity.<\/p>\n<h3>iOS<\/h3>\n<p>iOS 9 introduced several new search APIs that URL Navigation takes advantage of. There isn&#8217;t any additional set up unless you want to handle web links, but it&#8217;s a good idea to <a href=\"https:\/\/developer.xamarin.com\/guides\/ios\/platform_features\/introduction_to_ios9\/search\/\">read our full documentation<\/a> on all of the wonderful search APIs.<\/p>\n<h2>Creating Deep Links<\/h2>\n<p>The most common way of exposing a deep link to Google and Spotlight search is to create an AppLinkEntry in your application with metadata the system can use when a user navigates to a piece of content, such as the session details of the conference app. A good place to do this is on the OnAppearing method of a page.<\/p>\n<pre class=\"lang:csharp decode:true\">\r\nvar url = $\"http:\/\/evolve.xamarin.com\/session\/{session.Id.ToString()}\";\r\n\r\nvar entry = new AppLinkEntry\r\n{\r\n    Title = session.Title,\r\n    Description = session.Abstract,\r\n    AppLinkUri = new Uri(url, UriKind.RelativeOrAbsolute),\r\n    IsLinkActive = true,\r\n    Thumbnail = ImageSource.FromFile(\"Icon.png\")\r\n};\r\n\r\nentry.KeyValues.Add(\"contentType\", \"Session\");\r\nentry.KeyValues.Add(\"appName\", \"Evolve16\");\r\nentry.KeyValues.Add(\"companyName\", \"Xamarin\");\r\n<\/pre>\n<p>The session&#8217;s <strong>Id<\/strong> is used as the core identifier that can be parsed out when handling the link. With our <strong>AppLinkEntry<\/strong> created, we just have to register it with the system.<\/p>\n<pre class=\"lang:csharp decode:true\">\r\nApplication.Current.AppLinks.RegisterLink(entry);\r\n<\/pre>\n<h2>Handling Links<\/h2>\n<p>When your app&#8217;s users click on a link through Google Search, Spotlight Search, or a web link, the Xamarin.Form&#8217;s application subclass will receive a request to handle the <strong>Uri<\/strong>. We&#8217;ll simply override the <strong>OnAppLinkRequestReceived<\/strong>\u00a0method and handle navigation:<\/p>\n<pre class=\"lang:csharp decode:true\">\r\nprotected override void OnAppLinkRequestReceived(Uri uri)\r\n{\r\n    var data = uri.ToString().ToLowerInvariant();\r\n    \/\/only if deep linking\r\n    if (!data.Contains(\"\/session\/\"))\r\n        return;\r\n\r\n    var id = data.Substring(data.LastIndexOf(\"\/\", StringComparison.Ordinal) + 1);\r\n\r\n    \/\/Navigate based on id here.\r\n\r\n    base.OnAppLinkRequestReceived(uri);\r\n}\r\n<\/pre>\n<p>Some frameworks, such as <a href=\"https:\/\/github.com\/PrismLibrary\/Prism\">Prism<\/a>, have build in Uri navigation routing that could be utilized in this scenario.<\/p>\n<p>Here&#8217;s our final URL Navigation with just a few lines of code:\n<center><iframe width=\"640\" height=\"480\" src=\"https:\/\/www.youtube.com\/embed\/Z7E8GmMdDvc\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/center><\/p>\n<h2>Learn More<\/h2>\n<p>Be sure to watch my Xamarin Evolve 2016 session, <a href=\"https:\/\/www.youtube.com\/watch?v=XGmB-ELJjL0\">Mobile Search: Making Your Mobile Apps Stand Out<\/a>, to learn more about all of the innovative solutions available including URL Navigation for your Xamarin.Forms applications.\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Leveraging mobile search ensures your mobile app stands out on your users&#8217; devices. In addition to the powerful search built into each device, deep linking to your app from web content creates a fully connected experience to keep your users in your app and coming back for more. URL Navigation simplifies this entire process with [&hellip;]<\/p>\n","protected":false},"author":544,"featured_media":25852,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[4,16],"class_list":["post-25850","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","tag-xamarin-platform","tag-xamarin-forms"],"acf":[],"blog_post_summary":"<p>Leveraging mobile search ensures your mobile app stands out on your users&#8217; devices. In addition to the powerful search built into each device, deep linking to your app from web content creates a fully connected experience to keep your users in your app and coming back for more. URL Navigation simplifies this entire process with [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/25850","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/users\/544"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=25850"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/25850\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=25850"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=25850"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=25850"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}