How to integrate Deep Linking with Xamarin Forms

Chances are you already know what deep linking is and have already used it on a different platform. Unfortunately, there is not much out there on how to implement this in a Xamarin Forms app. This post is not to explain deep linking in great detail but merely focus on how to implement it within a xamarin forms app for iOS and Android.

Universal Links

Since everything is always changing in our world of technology, you might have heard a new way to implement deep linking called universal links, I would take the time to learn what this is, and how it might benefit your application before deciding which approach you need. Some applications might do just fine with the first approach (which we will cover) and others might find this approach not very helpful.

Here are some useful links on universal links:

https://evilmartians.com/chronicles/invitation-system-using-url-schemes-and-universal-links

View story at Medium.com

Creating Your Link

Before you can call the link which opens your app, we need to register that link.

iOS

In your iOS project go to the info.plist, we need to add a URL Type in the Advanced Tab (you can also set this up in the Source Tab).

The identifier needs to be a unique ID (this doesn’t have to be the same as your app ID, but still needs to be unique). Then your URL Scheme also needs to be extremely unique so that no other app would have the same one. This is where universal links differ, if another app has the same URL scheme, then that app would then own this link. Where as if you use a universal link, no other app could have the same one as you.

Responding to your link

Now that the link is registered, we need a way to tell when someone has clicked on the link and launched the app. In your AppDelegate.cs we need to override a method called OpenUrl.

As you can see above we are checking if the url is not null, if your app uses multiple links this is where you can check which link was actually clicked. Also we are checking for a parameter in the link called email, this is a very common need when it comes to deep linking, having the ability to pass in parameters with the link. On iOS we can use NSUrlQueryItem array to grab all the possible parameters that might be in the link. In this example I only have one and it’s called email. Once I find it I use the MessagingCenter to broadcast a message up to my UI layer and I’m passing the email up as well.

Android

On Android, we need to follow almost the same flow as iOS. The app must register a link, and then we have to capture when the app is launched via the link. So in the MainActivity.cs we’ll add an Intent Filter that will register the link, this will go above the class MainActivity and below the Activity. You’ll notice the DataScheme value must be the same as the URL Scheme from the iOS project.

Responding to your link

When ever your app is launched from a link, the OnCreate method for the activity that was used to register the Intent Filter will be called. Since in a Xamarin Forms app you probably only have the one main activity, this is the one that will be called. In here you can check for the Intent and grab your parameters.

Testing your Links

On iOS, you can open up Safari and type in your link and it will actually launch your app, if you’ve done everything correctly. The format of the link should be like this:

Your URL Scheme or the DataScheme from Android: testAppForLinks://

And that’s it, now if you need to pass any parameters you can add those at the end and separate each one with an ampersand.

Example of link with one parameter called “email”

testAppForLinks://email=myEmail@gmail.com

Example of link with two parameters, one called “email” and the other called “id”

testAppForLinks://email=myEmail@gmail.com&id=80

If you want to easily test your link for Android, you can down this app called Deep Link Tester https://play.google.com/store/apps/details?id=com.manoj.dlt&hl=en

Inside the app you just type your link in the text box and it will launch the link correctly.

Important Notes:

I would strongly recommend reading over the links I posted at the top in regard to universal links and some of the pitfalls with this approach of deep linking. One thing to note is if you send out an email with a deep link, almost every email client will strip out the link making it useless (except for the default mail app on iOS, and maybe a few more). In most cases you will need a webpage instead that will fire the deep link.

Leave a Reply