Triggering shouldStartLoadWithRequest with multiple window.location.href calls(使用多个 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用多个 window.location.href 调用触发 shouldStartLoadWithRequest
基础教程推荐
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01