iOS 8.0之后出来一个webView新框架WKWebView,比之前的UIWebView在效率,内存占用,加载速度上都快了不少,不过需求要做兼容 所以将UIWebView与WKWebView做了一个封装调用方式与UIWebView相同,可以继承做二次封装.
github地址:webView适配封装
初始化方法
ZFWebView *akm = [[ZFWebView alloc]initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 100)];
// akm.url = @”https://www.baidu.com“;
[akm loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@”https://www.baidu.com“]]];
akm.delegate = self;
[self.view addSubview:akm];
代理方法
/** webView开始加载 */
- (void)webViewDidStartLoad:(id)webView;
/** webView加载完成 */
- (void)webViewDidFinishLoad:(id)webView webViewHeight:(CGFloat)height;
/** webView加载失败 */
- (void)webView:(id)webView didFailLoadWithError:(NSError *)error;
/** webView内部跳转 */
- (void)webView:(id)webView shouldStartLoadWithUrl:(NSString *)url;
github上的demo中的AKMWebView做了一个继承使用,扩展了webView更多功能.
- webView添加手势获取webView上的图片
- (void)webViewClick:(UITapGestureRecognizer *)tap{
CGPoint point = [tap locationInView:self];
NSString *js = [NSString stringWithFormat:@”document.elementFromPoint(%f, %f).tagName”, point.x, point.y];
[self evaluateJavaScript:js completionHandler:^(id tagName) {
NSLog(@”%@”,tagName);
if ([tap isEqual:@”IMG”]) {
NSString *js = [NSString stringWithFormat:@”document.elementFromPoint(%f, %f).src”, point.x, point.y];
[self evaluateJavaScript:js completionHandler:^(id src) {
NSLog(@”%@”,src);
}];
}
}];
} - 获取webView内容高度
[self evaluateJavaScript:@”document.body.offsetHeight” completionHandler:^(id htmlHeight) {}];