Handle deep linking in .NET MAUI


    Handling deep linking in a .NET MAUI app allows you to handle incoming URLs or custom scheme links and navigate to specific pages or perform custom actions within your app. 

    Here's an outline of the steps involved in implementing deep linking in .NET MAUI:

    For Android:

    1. Define the Deep Linking URL Scheme:

    Or you can define multiple Url Schemes like:

    2. Handle deep links when a user taps on a link:

    There are 2 places you need to do logic code, one is in OnCreate (occur when your app is opened at the first time) and other is in OnNewIntent (occur when your app is active or in a foreground). Two methods are overridden in MainActivity.cs.

    See code below for better understanding:

    Remember set LaunchMode = LaunchMode.SingleTop of ActivityAttribute, otherwise an error would be occurred.

    For iOS:

    1. Define the Deep Linking URL Scheme:

        - Open the Info.plist file in the iOS project.

        - Add the following XML code to the file to register the custom scheme:

        - if you type a scheme like myapp://home on search bar on Safari, it will open your app, everything seems work fine, but if you want to open your app via a link like https://apple.com/home your app does not open because it's a universal link. Remember myapp is a scheme not a link.

    To use a universal link, you need to do some extra steps:

        - Create a json file named apple-app-site-association.json in a root folder in your web app.

        - Add the following code to the file:

        appID = {teamId}.{your app bundle id}, to get a teamId you will see it in Apple Developer (login and you will see it in the top right). To get bundleId, open your MAUI then double click on your project name see ApplicationId element it's your app bundleId. For example, your teamId = LL92LZLAJL and your app bundleId = com.companyname.mobileapp, so appId = LL92LZLAJL.com.companyname.mobileapp.

        - On https://developer.apple.com/account/resources/identifiers click on your app.

        - Then enable Associated Domains, then click Save and Download a file. Please note that if you used it before you have to replace that file by the newest.

        - Add xml file named Entitlements.plist in Platforms/iOS folder, and then add the following code in this file like below:

    After completing these below steps, when you click on a link it will open your app as expected.

    2. Handle deep links when a user taps on a link:

    Override ContinueUserActivity method in AppDelegate.cs.

    That's all for iOS.