balanv
balanv

Reputation: 10888

Communication Between WebView and WebPage - Titanium Studio

I am working in a Mobile project (using Titanium Studio), in which i have the below situation

1) My Mobile app contacts Rails backend to check some data, say check validity of a user id.

2) I found a way to load web pages in Mobile app, i.e., WebView

3) I could able to load the desired url, ex http://www.mydomain.com/checkuser?uid=20121 which would return data like status:success

But i need to read this data to show whether the response from server is a success or failure, how do i achieve this?

NOTE : The above mentioned scenario is an usecase, but actually what happens is i load a third party url in WebView and when user enters the data and submits, the result will be posted back to my website url.

EDIT : So the process is like below

1) WebView loaded with third party url like http://www.anyapiprovider.com/processdata

2) User will enter set of data in this web page and submits the page

3) The submitted data will be processed by the apiprovider and it returns data to my web page say http://www.mydomain.com/recievedata

This is the reason why i am not directly using GET using HTTPClient FYI : I tried to fire Ti.APP events right from the actual web page as suggested by few articles, but most of them says this will work only if the file loaded is in local and not a remote file. Reference Link

Please suggest me if my approach has to be improved.

Thanks

Upvotes: 0

Views: 2712

Answers (3)

balanv
balanv

Reputation: 10888

I found a solution for my problem

1) Load the http://www.mydomain.com/checkuser?uid=20121 in a webview

2) Let user enter and submit data to third party url

3) Recieve the response from third party url and print only <div id="result">status:success</div> in http://www.mydomain.com/recievedata page.

4) Add event listener for the web view as follows

webView.addEventListener('load', function(data) 
{ 
   //Add condition to check if the loaded web page has any div with id = result (to check if this is /recievedata page)
    alert(webView.evalJS("document.getElementById('result').innerHTML"));
});

The above alert would print the result status:success, read it in webview load event and take actions in web accordingly.

It works fine for me.

Upvotes: 1

JeffAtStepUp
JeffAtStepUp

Reputation: 405

If you don't want to follow Josiah's advice, then take a look at the Titanium docs on how to add a webview.addEventListener('load',... event listener and use webview.evalJS() to inject your own code into the third party HTML.

Maybe you can inject code to trap the submit event and fire a Ti event to trigger the downloading of data from your website.

Communication Between WebViews and Titanium - Remote Web Content Section

Upvotes: 2

Josiah Hester
Josiah Hester

Reputation: 6095

Instead of loading it in a WebView why not just GET it using a HTTP Client? This is much cleaner, and more standards based:

var xhr_get = Ti.Network.createHTTPClient({
    onload : function(e) {
        // Here is your "status:success" string
        var returnValue = this.responseText;
    },
    onerror : function(e) {
        Ti.API.info(this.responseText);
        Ti.API.info('CheckUserProgressOnActivity webservice failed with message : ' + e.error);
    }
});
xhr_get.open('GET', 'http://www.mydomain.com/checkuser?uid=20121');
xhr_get.send();

Upvotes: 0

Related Questions