美文网首页
iOS WKWebView高度自适应以及截获页面点击的url

iOS WKWebView高度自适应以及截获页面点击的url

作者: love埖落葙缌盡 | 来源:发表于2019-11-28 19:18 被阅读0次
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);//不允许跳转

}

}

相关文章

网友评论

      本文标题:iOS WKWebView高度自适应以及截获页面点击的url

      本文链接:https://www.haomeiwen.com/subject/qxciwctx.html