objective-C 在 UIWebView 中的 ajax 调用后获取 HTML 值



我想获取divUIWebView HTML button click后创建的值。

但问题是网页加载一次,HTML button ajax call后,它不会在UIWebView中再次加载,那么我如何访问ajax调用后生成的div值呢?

以下是 html 代码:

<a class="btn_class" params="{ &quot;container&quot;: &quot;betInfoTabReceipts&quot;, &quot;onComplete&quot;: &quot;ajaxOnCompleteTabs&quot;, &quot;onLoading&quot;: &quot;ajaxOnLoadTabs&quot;}" action="/some.something" onclick="$P('place_bet', {&quot;action&quot;: this.getAttribute('action'), &quot;params&quot;: this.getAttribute('params'), &quot;confirm&quot;: &quot;true&quot;}); return false;" id="btn_id" href="#"></a>

以下是 objective-c 代码:

    - (void)webViewDidFinishLoad:(UIWebView *)webView
{
    if([webView.request.URL.absoluteString isEqualToString:@"http://myurl.com/"])
    {
//some code that executes only once
}
}

我该如何解决这个问题?

您可以在调用 webview didFinishLoading delegate 方法时执行此 JavaScript 代码。像这样[webView stringByEvaluatingJavaScriptFromString:@""]然后你会得到基于 ajax 的请求回调

var s_ajaxListener = new Object();
s_ajaxListener.tempOpen = XMLHttpRequest.prototype.open;
s_ajaxListener.tempSend = XMLHttpRequest.prototype.send;
s_ajaxListener.callback = function () {
    window.location='mpajaxhandler://' + this.url;
};
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();
}

最新更新