iOS WKWebView高度自适应以及截获页面点击的url
2016-04-08 10:44
706 查看
首先来看下UIWebView的做法:
UIWebView *webView = [[UIWebViewalloc]initWithFrame:CGRectMake(0,0,WIDTH,0)];
webView.delegate =self;
[self.viewaddSubview:webView];
[webView loadHTMLString:html/*html内容*/ baseURL:nil];
-(void)webViewDidFinishLoad:(UIWebView*) webView {
//获取页面高度,并重置webview的frame
CGFloat documentHeight = [[webViewstringByEvaluatingJavaScriptFromString:@"document.getElementById(\"content\").offsetHeight;"]floatValue];
CGRect frame = webView.frame;
frame.size.height = documentHeight;
webView.frame = frame;
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest
*)request navigationType:(UIWebViewNavigationType)navigationType {
NSString* strRequest = request.URL.absoluteString;
if([strRequestisEqualToString:@"about:blank"]) {//主页面加载内容
returnYES;//允许跳转
} else {//截获页面里面的链接点击
//do something you want
returnNO;//不允许跳转
}
}
在来对应看下WKWebView:
WKWebView *wkWebview = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 0)];
wkWebview.navigationDelegate = self;
[self.view wkWebview];
[wkWebview loadHTMLString:html/*html内容*/ baseURL:nil];
- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecifiedWKNavigation
*)navigation {
[webView evaluateJavaScript:@"document.getElementById(\"content\").offsetHeight;"completionHandler:^(id_Nullable
result,NSError *_Nullable error) {
//获取页面高度,并重置webview的frame
CGFloat documentHeight = [resultdoubleValue];
CGRect frame = webView.frame;
frame.size.height = documentHeight;
webView.frame = frame;
}];
}
// 类似 UIWebView的 -webView: shouldStartLoadWithRequest: navigationType:
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction
*)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
NSString *strRequest = [navigationAction.request.URL.absoluteStringstringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
if([strRequestisEqualToString:@"about:blank"]) {//主页面加载内容
decisionHandler(WKNavigationActionPolicyAllow);//允许跳转
} else {//截获页面里面的链接点击
//do something you want
decisionHandler(WKNavigationActionPolicyCancel);//不允许跳转
}
}
高度自适应代码下载链接:http://download.csdn.net/detail/luco2008/9502733
UIWebView *webView = [[UIWebViewalloc]initWithFrame:CGRectMake(0,0,WIDTH,0)];
webView.delegate =self;
[self.viewaddSubview:webView];
[webView loadHTMLString:html/*html内容*/ baseURL:nil];
-(void)webViewDidFinishLoad:(UIWebView*) webView {
//获取页面高度,并重置webview的frame
CGFloat documentHeight = [[webViewstringByEvaluatingJavaScriptFromString:@"document.getElementById(\"content\").offsetHeight;"]floatValue];
CGRect frame = webView.frame;
frame.size.height = documentHeight;
webView.frame = frame;
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest
*)request navigationType:(UIWebViewNavigationType)navigationType {
NSString* strRequest = request.URL.absoluteString;
if([strRequestisEqualToString:@"about:blank"]) {//主页面加载内容
returnYES;//允许跳转
} else {//截获页面里面的链接点击
//do something you want
returnNO;//不允许跳转
}
}
在来对应看下WKWebView:
WKWebView *wkWebview = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 0)];
wkWebview.navigationDelegate = self;
[self.view wkWebview];
[wkWebview loadHTMLString:html/*html内容*/ baseURL:nil];
- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecifiedWKNavigation
*)navigation {
[webView evaluateJavaScript:@"document.getElementById(\"content\").offsetHeight;"completionHandler:^(id_Nullable
result,NSError *_Nullable error) {
//获取页面高度,并重置webview的frame
CGFloat documentHeight = [resultdoubleValue];
CGRect frame = webView.frame;
frame.size.height = documentHeight;
webView.frame = frame;
}];
}
// 类似 UIWebView的 -webView: shouldStartLoadWithRequest: navigationType:
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction
*)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
NSString *strRequest = [navigationAction.request.URL.absoluteStringstringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
if([strRequestisEqualToString:@"about:blank"]) {//主页面加载内容
decisionHandler(WKNavigationActionPolicyAllow);//允许跳转
} else {//截获页面里面的链接点击
//do something you want
decisionHandler(WKNavigationActionPolicyCancel);//不允许跳转
}
}
高度自适应代码下载链接:http://download.csdn.net/detail/luco2008/9502733
相关文章推荐
- appium环境搭建
- 苹果企业账号打包发布APP流程详解
- iOS学习笔记(十七)——文件操作(NSFileManager)
- Kotlin学习之object表达式和声明
- iOS开发的交互界面的搭建选择
- android学习-----自定义View 加载动画
- Android日历选择控件DatePickerDialog只显示年月
- TextView 实现跑马灯效果
- iOS开发~iOS9状态栏颜色设置
- applePay 项目的配置和集成
- Android 跨进程通信之:AIDL
- 在AndroidStudio中导入第三方类库
- ImageLoader的基本使用
- objective C中的@class, SEL , IMP等灵活机制
- Android跑马灯效果
- Drozer – Android APP安全评估工具(附测试案例)
- CMD编译JNI头文件方法 jobject/jclass 参数
- android图像处理
- android 自定义控件(底部icon点击效果)
- 深入理解Android之Binder通信机制