WKWebView遇到的问题

2020-03-18  本文已影响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);
    }
}
上一篇下一篇

猜你喜欢

热点阅读