关于WKWebView 开发中 的 一些 常规问题
2019-08-13 本文已影响0人
相敬如冰
点击link不会跳转的解决方案
// 在发送请求之前,决定是否跳转
- (void)webView:(WKWebView*)webView decidePolicyForNavigationAction:(WKNavigationAction*)navigationAction decisionHandler:(void(^)(WKNavigationActionPolicy))decisionHandler {
NSLog(@"decidePolicyForNavigationAction ==== %@", navigationAction);
//如果是跳转一个新页面
if(navigationAction.targetFrame==nil) {
[webViewloadRequest:navigationAction.request];
}
decisionHandler(WKNavigationActionPolicyAllow);
}
点击 邮箱 ,电话 无法 加载
其实这时候是不需要 跳转的,就让它cancel就可以了
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
//如果是 邮件 或者 电话 停止 跳转
if (navigationAction.request.URL.host == nil) {
NSArray *schemeArr = @[@"mailto",@"tel"];
if ([schemeArr containsObject:navigationAction.request.URL.scheme]) {
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
}
}
页面 禁止 捏合 放大缩小
// 7页面加载完调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation {
NSString *injectionJSString = @"var script = document.createElement('meta');"
"script.name = 'viewport';"
"script.content=\"width=device-width, user-scalable=no\";"
"document.getElementsByTagName('head')[0].appendChild(script);";
[webView evaluateJavaScript:injectionJSString completionHandler:nil];
}
网页原始的alert弹不出来
代理里 弹出 系统 的框
- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void(^)(void))completionHandler{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示"message:message?:@"" preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:([UIAlertAction actionWithTitle:@"确认"style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
completionHandler();
}])];
[self presentViewController:alertController animated:YES completion:nil];
}