Xamarin Forms Deep Linking and App Indexing with AppLinks

 

Xamarin Forms 2.3.0 added support for deep linking and app content indexing. Deep linking and app content indexing has always been available natively in Xamarin but it took a bit of work to implement and we can easily access it via Xamarin Forms.

Note: Its part of the 2.3.0+ branch of Xamarin Forms, however for Android you need to install the Xamarin.Forms.AppLinks package. For iOS you do not need to install any additional package and Windows doesn’t have app content indexing as far as I know.

If you are completely new to App Indexing and Deep Linking, I might suggest you start at App Discovery and Deep Linking Series Introduction to get an overview as to how everything works.

Introduction

As a quick introduction, App Indexing involves getting your app indexed in Google Search and Apple’s Mobile Search for a user to find. Deep linking involves when a user clicks on a search result and taking them to the right page and displaying the correct content within your app. AppLinks is only for Android and iOS. UWP does not have any app indexing capabilities available.

Xamarin Forms AppLinks uses CoreSpotlight for iOS and App Links for Android behind the scenes. If you want to see how to set these up natively you can look at Xamarin.iOS Mobile Search API and Firebase App Indexing. I highly recommend it as it explains the concepts that this package uses. Xamarin Forms AppLinks is just making the code easier to consume in a Xamarin Forms environment. In those two articles I walk through the process of setting it up natively and how it works.

Setup App Links

For Android make sure you install the Xamarin.Forms.AppLinks package and then call this method just above your Xamarin.Forms.Init call in your Android project.

AndroidAppLinks.Init(this);

Further to that make sure that you have correctly setup your app for App Indexing. Have a look at Firebase App Indexing that walks you through the steps.

In iOS you do not need to download any additional packages, nor call any Init function.

Create Your Deep Links

This is now the part where you register your links with either CoreSpotlight or App Indexing depending on which environment you are in.

var url = $"https://www.mywebsite.com/1234";
 
var appLinkEntry = new AppLinkEntry
{
   Title = "1234",
   Description = "Description of page,
   IsLinkActive = true,
   AppLinkUri = new Uri(url, UriKind.RelativeOrAbsolute),
   Thumbnail = ImageSource.FromFile("image.png")
};

// These are parameters that are shown in the search (especially the AppName)
appLinkEntry.KeyValues.Add("contentType", "Article");
appLinkEntry.KeyValues.Add("companyName", "Tesla");
appLinkEntry.KeyValues.Add("appName", "Tesla");

// This registers the link
Application.Current.AppLinks.RegisterLink(entry);

Handling Launch

When your app is called with one of these deep links you will need to redirect your app to the main page. In Android or iOS you will normally receive a call on ContinueUserActivity or onCreate, however Xamarin Forms App Links redirects all of this to a single function. Override this function in your App.cs (or App.xaml.cs) file in your Xamarin Forms Application class.

protected override void OnAppLinkRequestReceived(Uri uri)
{
     // Parse the uri to determine where in your app you need to go.
     // This is the same uri you registered above.

     base.OnAppLinkRequestReceived(uri);
}


XAMARIN WEEKLY NEWSLETTER

Subscribe to a hand-picked round up of the best Xamarin development links every week. Published every Friday. Free.

Signup to Weekly Xamarin

* I do not maintain or run this newsletter, but I do recommend it.

4 Comments

  1. Olamide James

    Hello Adam,
    Thanks for this post but i have another situation whereby there are two apps( App A and App B). I want to launch App B by clicking a button from App A and if App B is not installed, it should go to play store to find App B in other to install it.

    Is this possible.

    Regards

    1. Adam Pedley

      Yes, it is. On iOS you need to open up you info.plist (in xml) and you need to give the Uri Scheme of the other app. e.g. skype is skype://

      <key>LSApplicationQueriesSchemes</key>
      <array>
      <string>skype</string>
      </array>

      This is needed in iOS because the os won’t tell you if it opened successfully or not if you don’t add this.

      Next in iOS you get your uri (e.g. skype:// or whatever Uri Scheme you have for your app)

      var canOpen = UIApplication.SharedApplication.CanOpenUrl(new NSUrl(uri));

      if (!canOpen)
      return false;

      return UIApplication.SharedApplication.OpenUrl(new NSUrl(uri));

      In Android

      try
      {
      var aUri = Android.Net.Uri.Parse(uri.ToString());
      var intent = new Intent(Intent.ActionView, aUri);
      Xamarin.Forms.Forms.Context.StartActivity(intent);
      return true; // I was opened
      }
      catch (ActivityNotFoundException)
      {
      return false; // I was not found
      }

      In UWP

      return await Windows.System.Launcher.LaunchUriAsync(new Uri(uri));

      If these return false, then I would go Device.OpenUri(“uri to the app store listing”). From here it will open up a browser window with the URL.

      Also if you aren’t familiar with Uri Schemes, here is my article on it. https://xamarinhelp.com/uri-scheme/. You will need to make sure App B has a Uri Scheme:

  2. Giovanni

    Hello Adam,
    Congratulations for your website, it’s all very useful.
    I need to launch a web view inside in my app using a web deeplinks. in practice when a user accesses the web page from the mobile and click on a specific link within, this should trigger the opening of the app (if installed) at a specific point, or if the user does not have the app installed must be shown the store app download. this must be done for IOS, Android and Windows Phone. can you help me to do this using Xamarin Forms?

    Regards