Forum Xamarin Xamarin.Forms

Proper way to implement OnAppLinkRequestReceived in Xamarin Shell

philipzhengphilipzheng Member ✭✭
edited August 18 in Xamarin.Forms

Hi,

I am trying to get the my app to open a specific page in response to a link. In my MainActivity.cs I have the following intent filter, which seems to be working fine:

    [IntentFilter(new[] { Intent.ActionView },
        Categories = new[]
        {
            Intent.ActionView,
            Intent.CategoryDefault,
            Intent.CategoryBrowsable
        },
        DataScheme = "http",
        DataHost = "api.myapp.com",
        DataPathPrefix = "/posts/")]

In my App.xaml.cs I have

    protected async override void OnAppLinkRequestReceived(Uri uri)
        {
            string appDomain = "http://api.myapp.com/";
            // Removed extra code for brevity
            if(page == "posts")
            {
                if(MainPage == null)
                {
                    MainPage = new Shell();
                }

                int postId;
                if (int.TryParse(pageParameter, out postId))
                {
                    await Shell.Current.GoToAsync($"//Main/MyApp?postId={postId}");
                }
            }

            base.OnAppLinkRequestReceived(uri);
        }

I was wondering if it is normal that the MainPage is null (i.e. it is creating a new instance of the app instead of using an existing one) when it receives the link uri? If so, is it good to set the MainPage to a new Shell? What is the best practice?

Also, as a side question, why does typing in http://api.myapp.com/posts/7 work in my Android phone's google search app but not in chrome web browser? In chrome it just opens the website without giving me an option to open it in the app.

Thanks for reading this.

Best Answers

Answers

Sign In or Register to comment.