iPhone:webView:shouldStartLoadWithRequest:navigationType计时问题 [英] iPhone : webView:shouldStartLoadWithRequest:navigationType timing problem

查看:30
本文介绍了iPhone:webView:shouldStartLoadWithRequest:navigationType计时问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在加载的WebView和HTML字符串,我希望它可以捕获对链接的点击.为此,我需要使用webView:shouldStartLoadWithRequest:navigationType方法.问题在于,在完全加载HTML内容之前,会多次调用此方法,而我只想在那一刻开始捕获点击.问题是如何知道何时完全加载HTML内容?我以为这很简单,所以我创建了一个布尔值作为包含WebView的ViewController的iVar,并在调用loadHTMLString之后将其设置为YES.然后,在webView:shouldStartLoadWithRequest:navigationType中,我正在测试该布尔值是否为真,是否正在输出类似"OK"的情况.但是出现"OK"却没有单击链接=>失败.

I have a WebView which is loading and HTML String and I want it to catch clicks on links. For that I need to use the webView:shouldStartLoadWithRequest:navigationType method. The problem is that this method gets called multiple times before the HTML content is fully loaded and I only want to start catching clicks at that moment. The question is how to know when the HTML content is fully loaded ? I thought it was simple so I created a boolean as an iVar of the ViewController containing the WebView and I set it to YES after calling loadHTMLString. Then, in webView:shouldStartLoadWithRequest:navigationType I was testing if that boolean was true and if it was the case I was outputting something like "OK". But "OK" was appearing without clicking on a link => fail.

关于如何进行这项工作的任何想法吗?

Any idea on how I could make this work ?

预先感谢

推荐答案

您可以使用 webViewDidFinishLoad:委托方法来了解何时加载HTML.

You could use the webViewDidFinishLoad: delegate method to know when the HTML is loaded.

但是我宁愿使用另一种解决方案:
webView:shouldStartLoadWithRequest:navigationType:中,您可以按导航类型过滤请求:

But I'd rather use another solution:
In webView:shouldStartLoadWithRequest:navigationType: you can filter requests by navigation type:

if (navigationType == UIWebViewNavigationTypeLinkClicked) {
    // Catch links

    return NO; // if you want to cancel the request, else YES
}

这篇关于iPhone:webView:shouldStartLoadWithRequest:navigationType计时问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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