UIWebView加载完成事件
当 UIWebView (Iphone) 完成加载 URL 时是否可以启动事件。
我怎样才能找到 UIWebView 的当前 URL?
Is it possible to start an event when an UIWebView (Iphone) has finished loading the URL.
How can I find out, the current URL of the UIWebView?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,这是可能的。使用
UIWebViewDelegate
协议并在委托中实现以下方法:如果您需要 URL,可以使用属性
request
获取最后一个请求:Yes, that's possible. Use the
UIWebViewDelegate
protocol and implement the following method in your delegate:If you want the URL, you can get the last request using the property
request
:找到的解决方案都不适合我。
然后我发现这个例子至少比我在 Google/StackOverflow 上找到的任何其他解决方案都要好得多。
uiwebview-load-completion-tracker
None of the found solutions worked for me.
Then I found this example which at least works much better than any other solution I found on Google/StackOverflow.
uiwebview-load-completion-tracker
非常简单的方法:
第1步: 在头文件中设置委托
UIWebViewDelegate
。第2步:添加以下
webViewDidFinishLoad
方法来获取webview
的当前URLVery simple method:
Step 1: Set delegate
UIWebViewDelegate
in header file.Step 2: Add following
webViewDidFinishLoad
method to get current URL ofwebview
帕斯卡对“获取 URL”部分的回答很好。
然而!
来自 Apple 的 UIWebViewDelegate 文档:
“webViewDidFinishLoad:在 Web 视图完成加载框架后发送。”
框架!=页面。
当页面“完成加载”时调用 webViewDidFinishLoad。在此之前也可以调用多次。从 Amazon.com 加载的页面可能会生成十几个对 webViewDidFinishLoad 的调用。
如果您控制页面源,那么您可以对其进行负载测试,并且对于这种情况它将起作用。如果您只关心“页面加载完成后”被调用,那么 webViewDidFinishLoad 就足够了。
对于任意页面,使用任意 JavaScript,永久加载广告横幅,或自动滚动横幅,或实现视频游戏,页面“完成加载”的想法是错误的。
Pascal's answer for the "getting the URL" part is fine.
However!
From UIWebViewDelegate's documentation, from Apple:
"webViewDidFinishLoad: Sent after a web view finishes loading a frame."
Frame != Page.
webViewDidFinishLoad is called when the page is "done loading". It can also be called many times before then. Page loads from Amazon.com can generate a dozen calls to webViewDidFinishLoad.
If you control the page source, then you can make a load test for it, and it will work, for that case. If you only care about getting called "after the page is done loading", then webViewDidFinishLoad is adequate.
For arbitrary pages, with arbitrary JavaScript, loading ad banners in perpetuity, or autoscrolling banners, or implementing a video game, the very idea of a page being "done loading" is wrongheaded.