使用多个 window.location.href 调用触发 shouldStartLoadWithRequest [英] Triggering shouldStartLoadWithRequest with multiple window.location.href calls

查看:19
本文介绍了使用多个 window.location.href 调用触发 shouldStartLoadWithRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过 UIWebView 的 shouldStartLoadWithRequest 方法将 UIWebView 内的网页中的多个内容传递回我的 iPhone 应用程序.

Im trying to pass multiple things from a webpage inside a UIWebView back to my iPhone app via the shouldStartLoadWithRequest method of the UIWebView.

基本上我的网页调用 window.location.href = "command://foo=bar" 并且我能够在我的应用程序中拦截它没问题.现在,如果我创建一个循环并一次执行多个 window.location.href 调用,那么 shouldStartLoadWithRequest 似乎只被调用一次,并且它获得的调用是在循环结束时最后一次触发 window.location.href.

Basically my webpage calls window.location.href = "command://foo=bar" and i am able to intercept that in my app no problem. Now if i create a loop and do multiple window.location.href calls at once, then shouldStartLoadWithRequest only appears to get called on once and the call it gets is the very last firing of window.location.href at the end of the loop.

同样的事情发生在 Android 的 webview 上,只有最后一个 window.location.href 被处理.

The same thing happens with the webview for Android, only the last window.location.href gets processed.

推荐答案

iFrame = document.createElement("IFRAME");
iFrame.setAttribute("src", "command://foo=bar");
document.body.appendChild(iFrame); 
iFrame.parentNode.removeChild(iFrame);
iFrame = null;

所以这会创建一个 iframe,将它的源设置为我试图传递给应用程序的命令,然后一旦它附加到主体 shouldStartLoadWithRequest 被调用,然后我们从主体中删除 iframe,并将其设置为 null释放内存.

So this creates an iframe, sets its source to a command im trying to pass to the app, then as soon as its appended to the body shouldStartLoadWithRequest gets called, then we remove the iframe from the body, and set it to null to free up the memory.

我还使用 shouldOverrideUrlLoading 在 Android webview 上对此进行了测试,它也可以正常工作!

I also tested this on an Android webview using shouldOverrideUrlLoading and it also worked properly!

这篇关于使用多个 window.location.href 调用触发 shouldStartLoadWithRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆