0

I'm having a webpage that takes some information from the user, process it on the server side PHP and then gets the response back from the server.

Now I need to implement this (Including the web UI) into my Xamarin Forms app, so I thought about having a webview that loads the URL of the webpage, but now I can't figure out (Not even with googling) how to get the Javscript's resultant variable's value back to C#.

Here is a Javascript sample:-

xhttp.onreadystatechange = function () {
    if (this.readyState == 4 && this.status == 200) {
      let data = JSON.stringify(JSON.parse(this.responseText));
      // Now I want to pass 'data' variable to C#
    }
};

The app should wait till it receives this variable's value then executes other things, or until the user presses the back button to cancel the whole thing...

Any help is appreciated, thanks in advance. ^_^

2

1 Answer 1

1

First , create custom renderer for webview .

Then , wrap the js function with native api on each platform and add it into webview .

Invoke the function and pass the data , the method will aslo trigger in the renderer .

Refer https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/hybridwebview .

Sign up to request clarification or add additional context in comments.

6 Comments

The problem is that the webpage and javascript code is not local, I can't just wrap js code inside the Xamarin app itself, moreover, I can't really understand the documentation well. Maybe you can give example ?
I did check the sample link, but the same issue, it uses a local html page, on the other side, I want to load an external one, a url link...
However, I modified the custom renderer to allow using external links, So I will count this as the answer ^_^ One more thing, how to add a window property, in JS I can say if(window.hasOwnProperty("uid")) { doSomething(uid); } but how to assign the uid itself when loading the url ?!
do you mean how to pass the data when invoke the method ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.