Rooban Ponraj A
Rooban Ponraj A

Reputation: 281

How to get the height of the webpage that got loaded in UIWebView?

I have used following methods but still now not getting the correct value:

[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"] floatValue]

[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"]

webView.scrollView.contentSize.height

I have accessed all above in,

- (void)webViewDidFinishLoad:(UIWebView *)aWebView 
{

}

Upvotes: 0

Views: 1664

Answers (4)

Berlin Raj
Berlin Raj

Reputation: 124

use this

let height = webView.scrollView.contetSize.height

Upvotes: -1

Murali
Murali

Reputation: 1889

You can get this by 2 ways

method 1: Load your html like this

embedHTML = [embedHTML stringByAppendingFormat:
               @"<body><div id='size_div'>%@</div></body></html>",yourString];

And now in webViewDidFinishLoad method keep this statements...

NSString *output = [webView stringByEvaluatingJavaScriptFromString:
                      @"document.getElementById(\"size_div\").offsetHeight;"];  
NSLog(@"height: %@", output);

Method 2:

UIWebView *objWebView = [[UIWebView alloc] init];
for (UIScrollView *v in objWebView.subviews) {
    if ([v isKindOfClass:[UIScrollView class]])
    {
        CGSize requiredSize = v.contentSize;
    }
}

Upvotes: 2

VRN
VRN

Reputation: 13

You can get webview height by using

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    float height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"] floatValue];
}

Upvotes: 0

uruk
uruk

Reputation: 1306

I think webView.scrollView.contentSize is set automatically by the webView itself when loading the HTML. Have you tried it exactly like this:

- (void)webViewDidFinishLoad:(UIWebView *)webView {
     float h = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"] floatValue];

    // h is the height of the loaded webpage...

}

And don't forget to set the webView's delegate YourViewController: UIViewController <UIWebViewDelegate>

Upvotes: 0

Related Questions