How to get / process ajax response from final form stage in UIWebView?

I have a hybrid application that uses UIWebView to load an ajax / multi-step form from the server. Each step sends an ajax request to the server and receives an appropriate response from the server based on the input of the previous steps. In the last step, I would like to process the answer and use this information in my application for different things. Using Swift, what's the best way to achieve this? I obviously can get the initial answer when the page loads using NSURLCache, but I cannot figure out how to get the answers received between the ajax steps. Any ideas?

+4
source share
1 answer

As you can add your own Javascript to your UIWebView web page, you can enter a pad layer for Ajax calls after loading the start page.

In this mode, you load a custom URL that you can find in your UIWebView based on an Ajax call. When the delegate sees a fake URL, you record an Ajax call, but return that the download should not happen. Then you iterate over the Ajax source code, so everything works.

So, although you cannot detect Ajax calls, you can detect URL requests.

The following answer has everything you need: UIWebViewDelegate not tracking XMLHttpRequest?

** Update **

, . URL-, readyState http "//". .

, Ajax , UIWebView:

1) URL- mpAjaxHandler://URL

2) Ajax send() URL- mpAjaxHandlerDone://readyState: httpstatus/URL

, , ReadyState 4 HTTP- 200 .

, javascript , UIWebView , .

var s_ajaxListener = new Object();
s_ajaxListener.tempOpen = XMLHttpRequest.prototype.open;
s_ajaxListener.tempSend = XMLHttpRequest.prototype.send;
s_ajaxListener.callback = function () {
    console.log('mpAjaxHandler://' + this.url);
    window.location='mpAjaxHandler://' + this.url;
};
s_ajaxListener.callbackDone = function (state,status) {
    console.log('mpAjaxHandlerDone://' + state + ':' + status + '/' + this.url);
    window.location='mpAjaxHandlerDone://' + state + ':' + status + '/' + this.url;
};

// Added this function to catch the readyState changes and request
// fake page loads.
function override_onreadystatechange(){
  s_ajaxListener.callbackDone(this.readyState);
  this.original_onreadystatechange();
}

XMLHttpRequest.prototype.open = function(a,b) {
  if (!a) var a='';
  if (!b) var b='';
  s_ajaxListener.tempOpen.apply(this, arguments);
  s_ajaxListener.method = a;  
  s_ajaxListener.url = b;
  if (a.toLowerCase() == 'get') {
    s_ajaxListener.data = b.split('?');
    s_ajaxListener.data = s_ajaxListener.data[1];
  }
}

XMLHttpRequest.prototype.send = function(a,b) {
  if (!a) var a='';
  if (!b) var b='';
  s_ajaxListener.tempSend.apply(this, arguments);
  if(s_ajaxListener.method.toLowerCase() == 'post')s_ajaxListener.data = a;
  s_ajaxListener.callback();

  // Added this to intercept Ajax responses for a given send().
  this.original_onreadystatechange = this.onreadystatechange;
  this.onreadystatechange = override_onreadystatechange;
}

, loadDoc() W3Schools javascript. http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_first. , window.location=.

, .

+1

Source: https://habr.com/ru/post/1610009/


All Articles