我正在尝试通过UIWebView的shouldStartLoadWithRequest方法将多个内容从UIWebView内的网页传递回我的iPhone应用程序。
基本上,我的网页调用window.location.href = "command://foo=bar“,我可以在我的应用程序中拦截它,这是没有问题的。现在,如果我创建一个循环并一次执行多个window.location.href调用,那么shouldStartLoadWithRequest似乎只被调用一次,它得到的调用是在循环结束时最后一次触发window.location.href。
同样的事情也发生在Android的webview上,只有最后一个window.location.href会被处理。
发布于 2010-05-29 20:24:35
iFrame = document.createElement("IFRAME");
iFrame.setAttribute("src", "command://foo=bar");
document.body.appendChild(iFrame);
iFrame.parentNode.removeChild(iFrame);
iFrame = null;这将创建一个iframe,将其源设置为im尝试传递给应用程序的命令,然后一旦调用其附加到正文的shouldStartLoadWithRequest,我们就从正文中删除该iframe,并将其设置为null以释放内存。
我还使用shouldOverrideUrlLoading在一个Android webview上测试了它,它也工作正常!
发布于 2011-09-29 19:59:53
我也解决了这个问题,下面是我的解决方案。我的所有命令函数都使用此函数__js2oc(msg)通过shouldStartLoadWithRequest: P.S.将数据和事件传递给Objective-C。将“JavaScript:”替换为您使用的"appname:“触发器。
/* iPhone JS2Objective-C bridge interface */
var __js2oc_wait = 300; // min delay between calls in milliseconds
var __prev_t = 0;
function __js2oc(m) {
// It's a VERY NARROW Bridge so traffic must be throttled
var __now = new Date();
var __curr_t = __now.getTime();
var __diff_t = __curr_t - __prev_t;
if (__diff_t > __js2oc_wait) {
__prev_t = __curr_t;
window.location.href = "command:" + m;
} else {
__prev_t = __curr_t + __js2oc_wait - __diff_t;
setTimeout( function() {
window.location.href = "command:" + m;
}, (__js2oc_wait - __diff_t));
}
}发布于 2010-12-02 15:04:13
不,iframe的url改变不会触发shouldOverrideUrlLoading,至少在Android 2.2中不会。
https://stackoverflow.com/questions/2934789
复制相似问题