欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

WKWebView使用

程序员文章站 2022-03-20 17:23:05
...

JS和Native交互

WKWebView 通过 WKWebViewConfiguration 配合 WKNavigationDelegate 与JS进行调用。

js调用原生

native

- (WKWebViewConfiguration *)webConfig
{
    if (!_webConfig) {
        _webConfig = [WKWebViewConfiguration new];
        WKUserContentController *userController = [WKUserContentController new];
        [userController addScriptMessageHandler:self name:@"openInfo"];
        _webConfig.userContentController = userController;
    }
    return _webConfig;
}

- (WKWebView *)webView
{
    if (!_webView) {
        _webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:self.webConfig];
        _webView.navigationDelegate = self;
    }
    return _webView;
}

- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
    //js调用原生
    if ([message.name isEqualToString:@"openInfo"]) {
         id jsToNativeData = message.body;
        //js调用,拿到对应的参数做处理
    }
}

html

<!DOCTYPE html>
<html>

<head>
    <title>Test</title>
    <button style="width: 200px;height: 100px" onclick="openInfo()">点我</button>
</head>

<body>
    <script type="text/javascript">
    function openInfo() {
     
        if (navigator.platform.match(/iPhone|iPod|iPad/) {
                window.webkit.messageHandlers.openInfo.postMessage("Hello WebKit!");
                //关键是这里window.webkit.messageHandlers.[调用的名字].postMessage("调用的参数");
            }
        }
    </script>
</body>

</html>

注入JS

- (WKWebViewConfiguration *)webConfig
{
    if (!_webConfig) {
        _webConfig = [WKWebViewConfiguration new];
        WKUserContentController *userController = [WKUserContentController new];
        //js 代码
        NSString *js = @"js代码";
        //WKUserScriptInjectionTimeAtDocumentEnd 加载完注入
        //WKUserScriptInjectionTimeAtDocumentStart 加载开始注入
        WKUserScript *script = [[WKUserScript alloc] initWithSource:js injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:NO];
        [userController addUserScript:script];
        //js调用原生
        [userController addScriptMessageHandler:self name:@"openInfo"];
        _webConfig.userContentController = userController;
    }
    return _webConfig;
}

WKWebView的Title

UIWebView 获取document.title

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    NSString *theTitle=[webView stringByEvaluatingJavaScriptFromString:@"document.title"];
}

WKWebView 获取document.title

- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation
{
    NSString *title = webView.title;
}

WKWebView 禁止缩放方法

第一种

- (WKWebView *)webView
{
    if (!_webView) {
        _webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:self.webConfig];
        _webView.navigationDelegate = self;
        //设置scrollView.delegate = self;
        _webView.scrollView.delegate = self;
    }
    return _webView;
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return nil;
}

第二种

在html网页端禁止缩放,加入user-scalable=no,此处采用注入js的方法更改meta

- (WKWebViewConfiguration *)webConfig
{
    if (!_webConfig) {
        _webConfig = [WKWebViewConfiguration new];
        WKUserContentController *userController = [WKUserContentController new];
        NSString *js = @" $('meta[name=description]').remove(); $('head').append( '<meta name=\"viewport\" content=\"width=device-width, initial-scale=1,user-scalable=no\">' );";
        WKUserScript *script = [[WKUserScript alloc] initWithSource:js injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:NO];
        [userController addUserScript:script];
        [userController addScriptMessageHandler:self name:@"openInfo"];
        _webConfig.userContentController = userController;
    }
    return _webConfig;
}

WKWebView 不自动打开 target=“_blank”的链接

解决办法,参考http://*.com/

Add yourself as the WKNavigationDelegate

_webView.navigationDelegate = self;

and implement following code in the delegate callback decidePolicyForNavigationAction:decisionHandler:

应用外部打开链接

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
    if (!navigationAction.targetFrame) { 
        NSURL *url = navigationAction.request.URL;
        UIApplication *app = [UIApplication sharedApplication];
        if ([app canOpenURL:url]) {
            [app openURL:url];
        }
    }
    decisionHandler(WKNavigationActionPolicyAllow);
}

应用内部打开链接

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
    if (!navigationAction.targetFrame) {
        [webView loadRequest:navigationAction.request];
    }
    decisionHandler(WKNavigationActionPolicyAllow);
}