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

查看:129
本文介绍了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天全站免登陆