What is deep link?
Deep links enable you to:
- Launch the app through links – From an email link or website link, you could launch the app e.g. you could be browsing a website and click on a link that would launch the app giving you access to mobile specific functionality such as taking a photos allowing for seamless websites and mobile app integration.
- Bookmark the app – You could bookmark a page (by registering a link) so that that specific page in the app is searchable from iOS spotlight search or android google search bar.
Types of deep links
There are two ways of implementing deep links in Xamarin forms i.e
- Custom url schemes
- Xamarin app links – i.e. universal links (iOS) and android (Xamarin app links)
Xamarin app links is the more modern approach but each has their own advantages and disadvantages as compared below
|Custom Url schemes||Xamarin Deep Links|
|Setup||Easier. Less configuration required.||More complex – More configuration required e.g AASA file, assetlinks.json, iOS app services setup|
|App/Play store integration||Displays an error message when app is not installed.||Redirects user to App store/play store if app is not installed.|
|Url schemes||Well-known url schemes such as http/https are not allowed for iOS as outlined in this apple post||Can use well-known url schemes such as http/https|
Has website fallback if the link fails.
Custom url Schemes implementation
To setup, all that is needed is to define the url scheme in the following format john://doe or twitter://search or amazon://women. To pass parameters, add the values at the end of the url scheme e.g. amazon://women?id=14.
In info.plist file, click Add URL Type button and set the Identifier and URL Schemes. This is an example of the XML representation of the info.plist file after you have added the URL Type:
Handle the deep link, from AppDelegate.cs class by overriding the OpenUrl method and add the following implementation. To retrieve any parameters, use the urlComponents.QueryItems as shown below.
In MainActivity.cs class, register an intent that would be used to handle to deep link.
Override the OnNewIntent to handle the intent deep link as shown below
One thing to note, is the navigation back to xamarin forms from native using the OnAppLinkReceived method. Given deep links can only be received from native, we receive the deep links from xamarin.ios and xamarin.android then navigate to xamarin forms app.cs to execute the implementation.
To test the custom url schemes deep links, we will use email links. You will notice typing amazon://women in an email appears as plain unclickable text. To overcome this issue, two ways are proposed:
- Web redirect – Configure a web redirect to your url scheme e.g https://yourwebsite.com.au/mobile/config to a redirect to amazon://women which will launch the app.
- Using recognized hyperlink custom url – This is more of a quick workaround rather than a solution. It involves using recognized hyperlink formats such as wais://example.com as described in this xamarin deep linking post