UIWebView didFinishLoading 多次触发 [英] UIWebView didFinishLoading fires multiple times

查看:11
本文介绍了UIWebView didFinishLoading 多次触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码需要在 UIWebView 完成加载文档后运行.为此,我将 UIWebView 的委托设置为我的控制器,并实现了 webViewDidFinishLoading 方法.

I have some code that needs to run after the a UIWebView finishes loading a document. For that I've set the UIWebView's delegate to my controller, and implemented the webViewDidFinishLoading method.

这会被多次调用,具体取决于要加载的页面类型.我不确定是不是因为 ajax 请求、图像请求,甚至是 iframe.

This gets called multiple times, depending on the type of page to load. I'm not sure if it's because of ajax requests, requests for images, or maybe even iframes.

有没有办法告诉主请求已经完成,这意味着 HTML 已完全加载?

Is there a way to tell that the main request has finished, meaning the HTML is completely loaded?

或者也许延迟我的代码触发,直到所有这些事件都完成触发?

Or perhaps delay my code from firing until all of those events are done firing?

推荐答案

NSLog 跟踪加载开始和结束可能很有启发性(如果你还没有走这么远的话).

It could be enlightening (if you haven't gone this far yet) to NSLog a trace of load starts and finishes.

    - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
       NSLog(@"Loading: %@", [request URL]);
       return YES;
    }


    - (void)webViewDidFinishLoad:(UIWebView *)webView {
       NSLog(@"didFinish: %@; stillLoading: %@", [[webView request]URL],
            (webView.loading?@"YES":@"NO"));
    }


    - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
       NSLog(@"didFail: %@; stillLoading: %@", [[webView request]URL],
            (webView.loading?@"YES":@"NO"));
    }

我刚刚在我的一个项目中观看了对所有三个的调用,该项目从我的包中加载了一个帮助页面并包含嵌入式资源(外部 css、YUI!、图像).唯一通过的请求是初始页面加载,shouldStartLoadWithRequest 不会为任何依赖项调用.所以很好奇为什么你的 didFinishLoad 被多次调用.

I just watched the calls to all three in one of my projects which loads a help page from my bundle and contains embedded resources (external css, YUI!, images). The only request that comes through is the initial page load, shouldStartLoadWithRequest isn't called for any of the dependencies. So it is curious why your didFinishLoad is called multiple times.

您所看到的可能是由于重定向,或者如前所述,加载页面中的 ajax 调用.但是您至少应该能够平衡对 shouldStartLoad 和其他两个委托函数中的任何一个的调用,并能够确定加载何时完成.

Perhaps what you're seeing is due to redirects, or as mentioned, ajax calls within a loaded page. But you at least should be able balance calls to shouldStartLoad and either of the other two delegate functions and be able to determine when the loading is finished.

这篇关于UIWebView didFinishLoading 多次触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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