Xamarin App linking

SSinhg 286 Reputation points
2023-04-22T21:29:34.0166667+00:00

Hi - I am trying to do app linking something like shown in the video on Andriod. Can it be done without Prism? Any reference will be very helpful I'm using VS2022 and Android 12

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,301 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,459 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 69,386 Reputation points Microsoft Vendor
    2023-04-24T06:42:16.3166667+00:00

    Hello,

    Can it be done without Prism?

    Yes, Prism build in Uri navigation routing. If you want to navigate, you can get the URL, based on different value of URL, then use Xamarin.Forms Navigation to do it. Firstly, Install the Xamarin.Forms.AppLinks NuGet package into the Android application project. Then, In the MainActivity.cs file, add a declaration to use the Xamarin.Forms.Platform.Android.AppLinks namespace and add IntentFilter above the MainActivity like following code. As note, you can change the value of DataScheme, DataHost and DataHost by your needs.

    [Activity(Label = "FormsAppLinking", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize )]
        [IntentFilter(new[] { Intent.ActionView },
            Categories = new[]
            {
                Intent.ActionView,
                Intent.CategoryDefault,
                Intent.CategoryBrowsable
            },
            DataScheme = "http",
            DataHost = "deeplinking",
            DataPathPrefix = "/",
            AutoVerify = true)
        ]
        public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
        {
            protected override void OnCreate(Bundle savedInstanceState)
            {
                base.OnCreate(savedInstanceState);
               Xamarin.Essentials.Platform.Init(this, savedInstanceState);
                global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
    
               AndroidAppLinks.Init(this);
    
                LoadApplication(new App());
            }
    

    Then open your App.xaml.cs, and override the OnAppLinkRequestReceived method,

    protected override async void OnAppLinkRequestReceived(Uri uri)
            {
    
             var data = uri.ToString().ToLowerInvariant();
                //only if deep linking
                if (!data.Contains("id"))
                    return;
               var id = data.Substring(data.LastIndexOf("/", StringComparison.Ordinal) + 1);
               //Navigate  and transfer id here.
               await  MainPage.Navigation.PushModalAsync(new Page1(id));
    
               base.OnAppLinkRequestReceived(uri);
            }
    

    In the end, we can get the id in the Page1.

    public partial class Page1 : ContentPage
        {
            private string id;
           public Page1(string id)
            {
                InitializeComponent();
                this.id = id;
            }     
        }
    

    Here is a document about Application Indexing and Deep Linking, you can refer to it.

    Best Regards, Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful