[Objective-C] Get UIWebView html page size

A trick to get size and apply resize or other effects on your view.

Set your UIWebView delegate and create finishLoad method.
After that, using javascript, get document.height of your view and do what you want.

[code lang=”java” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]// in init
_WebView.delegate = YourClass; // probably self

– (void) webViewDidFinishLoad:(UIWebView *)webview
{
int h = [[_WebView stringByEvaluatingJavaScriptFromString:@"document.height"] floatValue];
_WebView.userInteractionEnabled = h > 600;
}[/code]

Remember to use UIWebView delegate in class declaration:

[code lang=”java” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]@interface YourClass : UIViewController <uiwebviewdelegate>;
@end[/code]

have fun.

 

Alberto Pasca

Software engineer @ Pirelli & C. S.p.A. with a strong passion for mobile  development, security, and connected things.