Skip to content
This repository has been archived by the owner on Oct 29, 2024. It is now read-only.

Redirecting back from third party website app not loaded in app tag #372

Closed
Akdimi opened this issue Jun 4, 2021 · 13 comments
Closed

Redirecting back from third party website app not loaded in app tag #372

Akdimi opened this issue Jun 4, 2021 · 13 comments

Comments

@Akdimi
Copy link

Akdimi commented Jun 4, 2021

Hi, when you navigate to third party website and then redirect back to the app the page is blank. When looking at the elements in html everything is there except there is nothing inside the app tag. Is there a way to handle this correctly? I tried almost everything like mapfallbacktofile in server startup etc.. i can redirect to the page but the app is not loaded into the app tag and there are no errors.

@arivera12
Copy link

arivera12 commented Jun 5, 2021

I ran exactly into this error, what I did was that I navigate to the default page/index page and pass params through app linking mechanism and read from the link a param which lets me know were to redirect. Look out for app linking on android and ios using xamarin. To pass or read any param from external apps or website you need tu enable app linking first.

@Akdimi
Copy link
Author

Akdimi commented Jun 5, 2021

I ran exactly into this error, what I did was that I navigate to the default page/index page and pass params through app linking mechanism and read from the link a param which lets me know were to redirect. Look out for app linking on android and ios using xamarin. To pass or read any param from external apps or website you need tu enable app linking first.

Thanks i will look into that👍. I am trying to do this now first in Wpf where i navigate to an external payment service that after payment redirects back to a page /orderdetails. When i do this in Webassmbly the page loads fine but in hybrid the page loads but app tag is empty and blank page ia rendered. Is this also what you had exactly?

@arivera12
Copy link

Yup, Exactly that, when you redirect back you will need to pass params in the query string of the url, example myapp.com?redirecto=/mypath

Remember that hybrid app has his entry point, and from your entry point you force the redirect once you use the applinking mechanism within ur xamarin app.

@arivera12
Copy link

Take note what you would need to setup your android and ios project so they react to a website link or app scheme and within that website link or app scheme you can provide params which were here you can give your app a desired action/task.

@Akdimi
Copy link
Author

Akdimi commented Jun 7, 2021

Yup, Exactly that, when you redirect back you will need to pass params in the query string of the url, example myapp.com?redirecto=/mypath

Remember that hybrid app has his entry point, and from your entry point you force the redirect once you use the applinking mechanism within ur xamarin app.

Is this also valid for Wpf? In the documentation they only mention IOs and Android. In Wpf I tried to pass in a query sting to the redirect Url like you said but still same problem.

@arivera12
Copy link

I haven't try wpf but since they are based on xamarin forms I think they should behave the same.

Did you overrided platform app linking and listened to it from xamarin side?

@Akdimi
Copy link
Author

Akdimi commented Jun 7, 2021

@arivera12 i didnt because since i didnt read anything in the documentation about Wpf i didnt know exactly how to proceed. I only did the query string part so far. I wil try it tomorrow and see if i can get it to work. Thank you for the guidance👍

@arivera12
Copy link

For example:

On Android you need to add intent filter to main activity

On top of your main activity

[IntentFilter(new[] { Intent.ActionView },
        Categories = new[]
        {
            Intent.CategoryDefault,
            Intent.CategoryBrowsable
        },
        DataScheme = "http",
        DataHost = "localhost")]

OnCreate

base.OnCreate(savedInstanceState);
            OnNewIntent(Intent);

Inside your main MainActivity

protected override void OnNewIntent(Intent intent)
        {
            base.OnNewIntent(intent);
        }

Now with this instruction you android app will react we any link having that uri is pressed.

On xamarin side you need to listen for app linking request on your app.cs

Heres is my implementation

 protected override void OnAppLinkRequestReceived(Uri uri)
        {
            var dataUri = uri.ToString().ToLowerInvariant();
            var paramsIndex = dataUri.IndexOf('?');
            if (paramsIndex != -1)
            {
                string querystring = dataUri.Substring(paramsIndex);
                if (!querystring.IsNullOrWhiteSpace())
                {
                    var dataParams = HttpUtility.ParseQueryString(querystring);
                    if (dataParams.IsNotNull() && dataParams.Count > 0)
                    {
                        Current.Properties.Clear();
                        foreach (var key in dataParams.Keys)
                        {
                            Current.Properties.Add(key.ToString(), dataParams[key.ToString()]);
                        }
                        new Action(async () => await Current.SavePropertiesAsync()).Invoke();
                    }
                }
            }
            base.OnAppLinkRequestReceived(uri);
        }

Then on your index.razor.cs on you OnInitializedAsync

if (Application.Current.Properties.ContainsKey("path"))
                {
                    var path = Application.Current.Properties["path"] + "?";
                    Application.Current.Properties.Remove("path");
                    foreach (var property in Application.Current.Properties)
                    {
                        if (!path.Contains("="))
                        {
                            path += $@"{property.Key}={property.Value}";
                        }
                        else
                        {
                            path += $@"&{property.Key}={property.Value}";
                        }
                    }
                    Application.Current.Properties.Clear();
                    await Application.Current.SavePropertiesAsync();
                    NavigationManager.NavigateTo(path);

You can pass as many params you want but for navigate in this implementation you will need to provide a param with name path so he reacts and navigates properly.

@arivera12
Copy link

I haven't yet setup ios or wpf but this works for android, once you hook up platform specific app linking, from xamarin side should be like an auto drive to the desired page.

@Akdimi
Copy link
Author

Akdimi commented Jun 12, 2021

@arivera12 Finally i got it to work on Android thank you very much for your help. Its only working when redirecting from external browser like chrome. The system prefered browser does not seem to invoke the intent when redirecting it took me another two days to figure this out😥. I modified the part where you clear all properties to only clear the redirecturl properties so that i dont lose user data because i had issues with saving user data to cookies on android and now i just store them in Application properties but i don't know if this is the right thing to do. But many thanks to you because i couldnt find anything on this on the internet after alot of searching👍

@Akdimi
Copy link
Author

Akdimi commented Jun 12, 2021

@arivera12 i forgot to mention that this doesnt work for Wpf in my case because Wpf is not supported by Xamarin Essentials. At least that's what i understood from searching around

@Akdimi Akdimi closed this as completed Jun 12, 2021
@arivera12
Copy link

arivera12 commented Jun 12, 2021

@Akdimi it took me like a month 😢 to figure this out.

Also there is no sample with this full implementation on the internet.

I did that merging 2-3 different articles.

But wait not so fast!

There is a port of xamarin essentials coming to Windows Desktop (WPF)!

Take a look of this xamarin/Essentials#1079

@arivera12
Copy link

@Akdimi consider using SecureStorage over Application.Current.Properties to store user data.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants