美文网首页
WKWebView遇到的问题

WKWebView遇到的问题

作者: 不是假发是桂 | 来源:发表于2020-03-18 15:14 被阅读0次

与js交互

[webView evaluateJavaScript:@"document.body.offsetHeight" completionHandler:^(id _Nullable offsetHeight, NSError * _Nullable error) {
        CGFloat  height = [offsetHeight floatValue];
        _webView.frame =  CGRectMake(0, _timeLab.frame.size.height + _timeLab.frame.origin.y + 20, FZM_SCREEN_WIDTH, height);
}];

字体变小

NSString *headerString = @"<header><meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=yes'></header>";
[_webView loadHTMLString: [headerString stringByAppendingString:_noticeDetailModel.noticeContent] baseURL:nil];

图片超出边界

NSString *headerString = @"<header><meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=yes'><style>img{max-width:100% !important; height:auto!important;}</style></header>";
[_webView loadHTMLString: [headerString stringByAppendingString:_noticeDetailModel.noticeContent] baseURL:nil];

代理方法decidePolicyForNavigationAction崩溃

这里崩溃没有任何信息,后来逐一排查才找到问题。就是必须调用decisionHandler

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
//else的情况没有调用decisionHandler就会崩溃
// if (navigationAction.navigationType == WKNavigationTypeLinkActivated) {
//     decisionHandler(WKNavigationActionPolicyCancel);
//  }
    if (navigationAction.navigationType == WKNavigationTypeLinkActivated) {
        decisionHandler(WKNavigationActionPolicyCancel);
    }else{
        decisionHandler(WKNavigationActionPolicyAllow);
    }
}

相关文章

网友评论

      本文标题:WKWebView遇到的问题

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