iPhone : webView:shouldStartLoadWithRequest:navigationType timing problem

元气小坏坏 提交于 2019-12-23 04:55:34

问题


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 ?

Thanks in advance


回答1:


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

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
}



回答2:


- (void)webViewDidFinishLoad:(UIWebView *)webViews{
}

this method will callwhen the HTML content is fully loaded. it may helps you.



来源:https://stackoverflow.com/questions/5619755/iphone-webviewshouldstartloadwithrequestnavigationtype-timing-problem

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!