priore

UIWebView get vertical height

Mar 31st, 2017
571
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // file: "UIWebView+Height.h"
  2.  
  3. #import <UIKit/UIKit.h>
  4.  
  5. typedef void(^WebViewContentLoaded)(NSString *html, CGSize scrollSize);
  6.  
  7. @interface UIWebView (Height) <UIWebViewDelegate>
  8.  
  9. @property (nonatomic, copy) WebViewContentLoaded webLoadedBlock;
  10.  
  11. - (void)loadURL:(NSURL*)url completion:(WebViewContentLoaded)completion;
  12.  
  13. @end
  14.  
  15. // file: "UIWebView+Height.m"
  16.  
  17. #import "UIWebView+Height.h"
  18. #import <objc/runtime.h>
  19.  
  20. @implementation UIWebView (Height)
  21.  
  22. - (WebViewContentLoaded )webLoadedBlock {
  23.     return objc_getAssociatedObject(self, @"webLoadedBlock");
  24. }
  25.  
  26. - (void)setWebLoadedBlock:(WebViewContentLoaded)webLoadedBlock {
  27.     objc_setAssociatedObject(self, @"webLoadedBlock", webLoadedBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
  28. }
  29.  
  30. - (void)loadURL:(NSURL*)url completion:(WebViewContentLoaded)completion
  31. {
  32.     if (self.superview == nil) {
  33.         [[UIApplication sharedApplication].keyWindow insertSubview:self atIndex:0];
  34.     }
  35.    
  36.     self.webLoadedBlock = completion;
  37.     self.delegate = self;
  38.    
  39.     NSURLRequest *req = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60];
  40.     [self loadRequest:req];
  41. }
  42.  
  43. #pragma mark - UIWebView Delegate
  44.  
  45. - (void)webViewDidFinishLoad:(UIWebView *)webView
  46. {
  47.     // UIWebView object has fully loaded.
  48.     NSString *state = [webView stringByEvaluatingJavaScriptFromString:@"document.readyState"];
  49.     if ([state isEqualToString:@"complete"])
  50.     {
  51.         // recupera l'altezza del contenuto della webview
  52.         NSString *html = [webView stringByEvaluatingJavaScriptFromString: @"document.body.innerHTML"];
  53.         float width = [[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.scrollWidth"] floatValue];
  54.         float height = [[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.scrollHeight"] floatValue];
  55.        
  56.         if ([self.superview isEqual:[UIApplication sharedApplication].keyWindow]) {
  57.             [self removeFromSuperview], self.delegate = nil;
  58.         }
  59.        
  60.         if (self.webLoadedBlock)
  61.             self.webLoadedBlock(html, CGSizeMake(width, height));
  62.     }
  63. }
  64.  
  65. - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
  66. {
  67.     if (self.webLoadedBlock)
  68.         self.webLoadedBlock(nil, CGSizeZero);
  69. }
  70.  
  71. @end
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×