How to get the content height of webView from iOS

Posted by tywin on Sun, 31 May 2020 08:31:08 +0200

In order to obtain the height of WebView, a scheme has been written before -- to update the height in real time by monitoring the changes of scrollView of WebView
Attach the link: iOS [ultimate solution] accurately obtains the content height and adaptive height of webView
This paper gives a second solution to the inaccuracy of content Height Acquisition caused by a large number of pictures and moving pictures in the web page
Direct code:
#pragma mark -- add JS event to each Img tag -————
-(void)webViewDidFinishLoad:(UIWebView *)webView
    NSString *js = @"var imgs = document.getElementsByTagName(\"img\");"
    "for(var i=0;i<imgs.length;i++){"
    "  var img = imgs[i];"
    "  img.onload=function(){window.location.href=('yourAppUrlSchemes://ImgLoaded');}"
    //yourAppUrlSchemes is in info.plist  -Current URL Schemes set in URL types 
    [webView stringByEvaluatingJavaScriptFromString:js];

#pragma mark ====================== intercept the request of webview to get the position of the last visible element in the web page===============
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSString *urlString = [[request URL] absoluteString];
    urlString = [urlString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSArray *urlComps = [urlString componentsSeparatedByString:@"://"];
    if (urlComps.count==2&&[urlComps[1] isEqualToString:@"ImgLoaded"]) {
        //imgload image loading completed, get height again
        CGSize fittingSize = [_webView sizeThatFits:CGSizeZero];
        //What I get here is the position of the penultimate tag in the web page. You can set the position of the penultimate tag according to the structure of the web page to be loaded, which can represent the height of the whole web page
        CGFloat lastHeight =[[_webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName(\"body\")[0].children[document.getElementsByTagName(\"body\")[0].children.length-3].offsetTop"] floatValue];
        _webView.frame = CGRectMake(0, 0, fittingSize.width, lastHeight);

        [self.tableView beginUpdates];
        [self.tableView setTableHeaderView:_webView];
        [self.tableView endUpdates];
    return YES;
The above idea is to add an onload event to each img tag. The event execution content is to send a notice to the APP itself. The APP intercepts the request to be sent in the shouldStartLoadWithRequest callback and recalculates the height of the page content.
This is a handy method, not flexible enough. It has been proved to be able to solve the problem of height acquisition of fixed structure web pages. For uncertain structure web pages, it is recommended not to use this method, which is prone to errors.
The gods have the perfect way to solve these problems. Please contact me in time to help more people~

Author: choumanon
Source: Jianshu
The copyright belongs to the author. For commercial reprint, please contact the author for authorization. For non-commercial reprint, please indicate the source.

Topics: iOS