Last active
March 3, 2017 18:48
-
-
Save buranmert/204decdb5c5f0f06a77129de9d45936e to your computer and use it in GitHub Desktop.
WebView contentSize inspection
This file contains hidden or 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
#import "WebViewViewController.h" | |
#import <WebKit/WebKit.h> | |
@interface UIView (AGLayoutExtensions) | |
- (void)addSubviewWithPinnedEdges:(UIView *)subview; | |
@end | |
@implementation UIView (AGLayoutExtensions) | |
- (void)addSubviewWithPinnedEdges:(UIView *)subview { | |
[self addSubview:subview]; | |
[subview setTranslatesAutoresizingMaskIntoConstraints:NO]; | |
[self addConstraints:[NSLayoutConstraint | |
constraintsWithVisualFormat:@"H:|-0-[subview]-0-|" | |
options:NSLayoutFormatDirectionLeadingToTrailing | |
metrics:nil | |
views:NSDictionaryOfVariableBindings(subview)]]; | |
[self addConstraints:[NSLayoutConstraint | |
constraintsWithVisualFormat:@"V:|-0-[subview]-0-|" | |
options:NSLayoutFormatDirectionLeadingToTrailing | |
metrics:nil | |
views:NSDictionaryOfVariableBindings(subview)]]; | |
} | |
@end | |
@interface WebViewViewController () <WKNavigationDelegate, WKUIDelegate> | |
@property (nonatomic, weak) WKWebView *webView; | |
@end | |
@implementation WebViewViewController | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectMake(50.f, 50.f, 500.f, 500.f)]; | |
webView.navigationDelegate = self; | |
webView.UIDelegate = self; | |
[self.view addSubviewWithPinnedEdges:webView]; | |
self.webView = webView; | |
NSURL *url = [NSURL URLWithString:@"https://www.apple.com"]; | |
NSURLRequest *request = [NSURLRequest requestWithURL:url]; | |
[self.webView loadRequest:request]; | |
} | |
- (void)viewDidLayoutSubviews { | |
[super viewDidLayoutSubviews]; | |
[self.webView evaluateJavaScript:@"var body = document.body; var html = document.documentElement; Math.max(body.scrollHeight,body.offsetHeight,html.clientHeight,html.offsetHeight);" | |
completionHandler:^(id _Nullable obj, NSError * _Nullable error) { | |
NSLog(@"viewDidLayoutSubviews"); | |
NSLog(@"%@", ((NSNumber *)obj).stringValue); // 735.0 | |
}]; | |
} | |
- (void)webView:(WKWebView *)webView didCommitNavigation:(null_unspecified WKNavigation *)navigation { | |
[self.webView evaluateJavaScript:@"var body = document.body; var html = document.documentElement; Math.max(body.scrollHeight,body.offsetHeight,html.clientHeight,html.offsetHeight);" | |
completionHandler:^(id _Nullable obj, NSError * _Nullable error) { | |
NSLog(@"didCommitNavigation"); | |
NSLog(@"%@", ((NSNumber *)obj).stringValue); // (null) | |
}]; | |
} | |
/*! @abstract Invoked when a main frame navigation completes. | |
@param webView The web view invoking the delegate method. | |
@param navigation The navigation. | |
*/ | |
- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation { | |
[self.webView evaluateJavaScript:@"var body = document.body; var html = document.documentElement; Math.max(body.scrollHeight,body.offsetHeight,html.clientHeight,html.offsetHeight);" | |
completionHandler:^(id _Nullable obj, NSError * _Nullable error) { | |
NSLog(@"didFinishNavigation"); | |
NSLog(@"%@", ((NSNumber *)obj).stringValue); // 1487.0 | |
}]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
is this something embedded into a Phonegap / Cordova app? or a standalone app (which doesn't show the problem)?