美文网首页
iOS WK_UIWebview(JS)

iOS WK_UIWebview(JS)

作者: 学不来的凡人 | 来源:发表于2021-04-07 09:11 被阅读0次
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>App与WebView交互</title>
</head>
<body>
<button style="width: 100%; height: 100px;" onclick="buttonClick()">点击购买</button>
</body>
<script>
    //按钮点击事件
    function buttonClick() {
        //传递的信息
        var jsonStr = '{"id":"666", "message":"我是传递的数据"}';

        //UIWebView使用
        getMessage(jsonStr);

       //WKWebView使用
       //使用下方方法,会报错,为使界面执行逻辑通畅,因此使用try-catch
        try {
            window.webkit.messageHandlers.getMessage.postMessage(jsonStr)
        } catch(error) {
            console.log(error)
        }
    }
    function getMessage(json){
        //空方法
    }
</script>
</html>

1、WKWebView

//JS调用的OC回调方法
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{
    
//    if ([message.name isEqualToString:@"getMessage"]) {
//        NSString *cookiesStr = message.body;
//        NSLog(@"当前的cookie为: %@", cookiesStr);
//        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"JS调用的OC回调方法" preferredStyle:UIAlertControllerStyleAlert];
//        UIAlertAction *action = [UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleCancel handler:nil];
//        [alert addAction:action];
//        [self presentViewController:alert animated:YES completion:nil];
//    }
    
    TwoViewController *twoVC = [TwoViewController new];
    [self presentViewController:twoVC animated:YES completion:nil];
    
    
}
- (WKWebView *)webview{
    if (!_webview) {
        WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
        WKUserContentController *userController = [[WKUserContentController alloc] init];
        configuration.userContentController = userController;

        [userController addScriptMessageHandler:(id <WKScriptMessageHandler>)self name:@"getMessage"];
        _webview = [[WKWebView alloc]initWithFrame:self.view.bounds configuration:configuration];
        NSString *path = [[[NSBundle mainBundle] bundlePath]  stringByAppendingPathComponent:@"test.html"];
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]];
        [_webview loadRequest:request];
        [_webview setOpaque:NO];
        _webview.scrollView.decelerationRate = UIScrollViewDecelerationRateNormal;
        _webview.UIDelegate = self;
        _webview.navigationDelegate = self;
        _webview.autoresizingMask = UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight;
        _webview.scrollView.bounces = NO;
    }
    return _webview;
}

2、UIWebView

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    //核心方法如下
    JSContext *content = [self.webview valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    //此处的getMessage和JS方法中的getMessage名称一致.
    content[@"getMessage"] = ^() {
        NSArray *arguments = [JSContext currentArguments];
        for (JSValue *jsValue in arguments) {
            NSLog(@"=======%@",jsValue);
        }
    };
}

- (UIWebView *)webview{
    if (!_webview) {
        _webview = [[UIWebView alloc]initWithFrame:self.view.bounds];
        NSString *path = [[[NSBundle mainBundle] bundlePath]  stringByAppendingPathComponent:@"test.html"];
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]];
        [_webview loadRequest:request];
        [_webview setOpaque:NO];
        _webview.delegate = self;
        _webview.autoresizingMask = UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight;
        _webview.scrollView.bounces = NO;
    }
    return _webview;
}

相关文章

网友评论

      本文标题:iOS WK_UIWebview(JS)

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