How to open localhost url in app

JornAggrorJornAggror Member ✭✭
edited August 30 in Xamarin.Forms

I am converting a web-app (phonegap) to xamarin forms. The first page is a log in screen which is a website that is opened in a webview embedded in the app. Opening this page goes fine, however after the login is successful, it will redirect me to (http://)localhost/localhost/online/src

What I want to achieve is that after this login, is that the app is able to 'pick up' this localhost url and read its parameters (it contains oauth code).
LogonPage
<WebView x:Name="LogonPage" Source="{Binding LogonPageUrl}}" />

From what I understand I should be able to do something like this topic (https://)forums.xamarin.com/discussion/36443/xamarin-forms-custom-url-schemes (I am not allowed to post links yet).
This would add an Intent on my MainActivity (Android for now), but so far my webview just gets redirect to the localhost page and the app doesn't seem to react to it.

MainActivity
[IntentFilter( new[] { Android.Content.Intent.ActionView }, Categories = new[] { Android.Content.Intent.CategoryDefault, Android.Content.Intent.CategoryBrowsable }, DataSchemes = new[] { "http" }, DataHosts = new[] { "http://localhost/online/src" } )]

How would I go and setup that my app reachts to the navifated localhost url so that I can process the parameters. I have suggestions that I could use a QueryProperty attribute to read out the parameter in the URL, but how would I link the localhost url to for instance an Authorized page?

Some notes:
1. I am not allowed to change the redirect url after login.

Best Answer

  • JornAggrorJornAggror ✭✭
    edited September 3 Accepted Answer

    I managed to work around this by using the navigate handler. This basically handles every navigation change in the webview. If the url contains my 'localhost' url, I can move further from there on out. It may not be the prettiest but at least I have something working for now.

    void Handle_Navigating(object sender, Xamarin.Forms.WebNavigatingEventArgs e) { if (e.Url.Contains("localhost/online")) { //Do stuff } }

Answers

  • HarshitaHarshita INMember ✭✭✭✭

    localhost will not work inside a virtual or physical device.Because both are different platform.So you need to host your site.then open with web view.

  • JornAggrorJornAggror Member ✭✭

    Thanks for replying.

    Lets say I don't have to use localhost, but instead have my redirect url go to 'myApp://authorizedPage'. Would that work with an embedded browser? Since the app is already open this option might not be viable.
    For instance for Android I would do this:

  • JornAggrorJornAggror Member ✭✭
    edited September 3 Accepted Answer

    I managed to work around this by using the navigate handler. This basically handles every navigation change in the webview. If the url contains my 'localhost' url, I can move further from there on out. It may not be the prettiest but at least I have something working for now.

    void Handle_Navigating(object sender, Xamarin.Forms.WebNavigatingEventArgs e) { if (e.Url.Contains("localhost/online")) { //Do stuff } }

Sign In or Register to comment.