UIWebView加载完成事件

发布于 2024-08-10 05:22:17 字数 82 浏览 5 评论 0原文

当 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

水波映月 2024-08-17 05:22:17

是的,这是可能的。使用 UIWebViewDelegate 协议并在委托中实现以下方法:

- (void)webViewDidFinishLoad:(UIWebView *)webView

如果您需要 URL,可以使用属性 request 获取最后一个请求:

webView.request.URL

Yes, that's possible. Use the UIWebViewDelegate protocol and implement the following method in your delegate:

- (void)webViewDidFinishLoad:(UIWebView *)webView

If you want the URL, you can get the last request using the property request:

webView.request.URL
谷夏 2024-08-17 05:22:17

找到的解决方案都不适合我。

然后我发现这个例子至少比我在 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

空宴 2024-08-17 05:22:17

非常简单的方法:

第1步: 在头文件中设置委托UIWebViewDelegate

第2步:添加以下webViewDidFinishLoad方法来获取webview的当前URL

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSLog(@"Current URL = %@",webView.request.URL);

    //-- Add further custom actions if needed 
}

Very simple method:

Step 1: Set delegate UIWebViewDelegate in header file.

Step 2: Add following webViewDidFinishLoad method to get current URL of webview

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSLog(@"Current URL = %@",webView.request.URL);

    //-- Add further custom actions if needed 
}
病女 2024-08-17 05:22:17

帕斯卡对“获取 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文