Last active
December 14, 2015 16:39
-
-
Save stephenkeep/5116749 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// UIWebView+TrueHeight.h | |
// RAWebViewTrueHeight | |
// | |
// Created by Stephen Keep on 08/03/2013. | |
// Copyright (c) 2013 Stephen Keep. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
@interface UIWebView (TrueHeight) <UIWebViewDelegate> | |
@property (nonatomic, strong) UIViewController* parentViewController; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// UIWebView+TrueHeight.m | |
// RAWebViewTrueHeight | |
// | |
// Created by Stephen Keep on 08/03/2013. | |
// Copyright (c) 2013 Stephen Keep. All rights reserved. | |
// | |
#import "UIWebView+TrueHeight.h" | |
#import <objc/runtime.h> | |
static char const * const parentViewControllerKey = "parentViewController"; | |
@implementation UIWebView (TrueHeight) | |
@dynamic parentViewController; | |
- (UIViewController *)parentViewController { | |
return objc_getAssociatedObject(self, parentViewControllerKey); | |
} | |
- (void)setParentViewController:(id)parentViewController { | |
objc_setAssociatedObject(self, parentViewControllerKey, parentViewController, OBJC_ASSOCIATION_RETAIN_NONATOMIC); | |
} | |
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { | |
NSURL *url = request.URL; | |
if ([url.scheme isEqual:@"height"]) { | |
[self webViewDidFinishLoadWithTrueHeight:[url.host floatValue]]; | |
return NO; | |
} | |
return YES; | |
} | |
-(void)webViewDidFinishLoad:(UIWebView *)webView { | |
float height = [[self stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"] floatValue]; | |
[self webViewDidFinishLoadWithTrueHeight:height]; | |
NSString *jquery = [[NSBundle mainBundle] pathForResource:@"jquery.min" ofType:@"js"]; | |
[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"var script = document.createElement('script');" | |
"script.type = 'text/javascript';" | |
"script.src = '%@';" | |
"document.getElementsByTagName('head')[0].appendChild(script);",jquery]]; | |
[webView stringByEvaluatingJavaScriptFromString:@"var script = document.createElement('script');" | |
"script.type = 'text/javascript';" | |
"script.text = \"$(document).ready(function() {" | |
"window.location = 'height://'+$(document).height();" | |
"})\";" | |
"$(\"body\").append(script);"]; | |
} | |
-(void)webViewDidFinishLoadWithTrueHeight:(float)height { | |
if (self.parentViewController && [self.parentViewController respondsToSelector:@selector(webViewDidFinishLoadWithTrueHeight:)]) { | |
[self.parentViewController performSelector:@selector(webViewDidFinishLoadWithTrueHeight:) withObject:[NSNumber numberWithFloat:height]]; | |
} | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.m | |
// RAWebViewTrueHeight | |
// | |
// Created by Stephen Keep on 08/03/2013. | |
// Copyright (c) 2013 Stephen Keep. All rights reserved. | |
// | |
#import "ViewController.h" | |
#import "UIWebView+TrueHeight.h" | |
@interface ViewController () | |
@property (nonatomic, strong) IBOutlet UIWebView *webView; | |
@end | |
@implementation ViewController | |
-(void)viewDidLoad { | |
[super viewDidLoad]; | |
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.redant.com"]]]; | |
self.webView.parentViewController = self; | |
self.webView.delegate = self.webView; | |
} | |
-(void)webViewDidFinishLoadWithTrueHeight:(NSNumber *)height { | |
NSLog(@"vc: %.2f",[height floatValue]); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment