WKWebView JS与OC间相互传值调用

栏目: IOS · 发布时间: 7年前

内容简介:1、导入WebKit.framework2、声明WKWebView

1、导入WebKit.framework

WKWebView JS与OC间相互传值调用

2、声明WKWebView

@interface ViewController ()
@property (nonatomic, strong) WKWebView * webView;

@end

JS调用OC原生方法

1、前端在js文件中实现

这里要注意messageHandlers后面跟着的deviceInfo要与原生的定义的一致。

//有参数
window.webkit.messageHandlers.deviceInfo.postMessage({
        "body": "buttonActionMessage"
});

//无参数
window.webkit.messageHandlers.deviceInfo.postMessage("");

2、iOS 实现WKScriptMessageHandler协议

@interface ViewController ()<WKScriptMessageHandler>
@property (nonatomic, strong) WKWebView * webView;

@end

3、iOS 初始化WKWebView

这里注意[userController addScriptMessageHandler:self name:@”deviceInfo”];

这个方法里的deviceInfo要与前端定义的一致。

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
    WKUserContentController *userController = [[WKUserContentController alloc] init];
    configuration.userContentController = userController;
    self.webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:configuration];
    [userController addScriptMessageHandler:self name:@"deviceInfo"];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:kURL]];
    [self.webView loadRequest:request];
    [self.view addSubview:self.webView];
}

4、iOS 实现WKScriptMessageHandler协议方法

- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
    
    if ([message.name isEqualToString:@"deviceInfo"]) {
        
        NSString *deviceInfoStr = message.body;
        NSLog(@"deviceInfo:%@", deviceInfoStr);
        
        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];
    }
    
}

message.body就是前端传来的参数

OC调用JS方法

1、实现WKNavigationDelegate协议

@interface ViewController ()<WKScriptMessageHandler,WKNavigationDelegate>
@property (nonatomic, strong) WKWebView * webView;

@end

2、实现WKNavigationDelegate协议的- webView: didFinishNavigation:方法

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    
}

3、OC调用JS方法

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    
    [webView evaluateJavaScript:@"deviceInfo('8.3')" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
        NSLog(@"ok");
    }];
    
}

其中deviceInfo(‘8.3’)为前端给出的JS方法名8.3是需要传给前端的参数

4、注意事项

以下错误的解决方法

WKWebView JS与OC间相互传值调用

- (void)evaluateJavaScript:(NSString *)javaScriptString completionHandler:(void (^ _Nullable)(_Nullable id, NSError * _Nullable error))completionHandler;

一定要在网页加载完成后调用,否则报错

JS方法一定要放在最外层

WKWebView JS与OC间相互传值调用

图片出处不详,请作者看到与我联系,侵删


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

人类思维如何与互联网共同进化

人类思维如何与互联网共同进化

[美] 约翰·布罗克曼 / 付晓光 / 浙江人民出版社 / 2017-3 / 79.90元

➢人类是否因互联网的诞生进入了公平竞争的场域? “黑天鹅事件”频频发生,我们的预测能力是否正在退化? 智人的第四阶段有哪些特征? 全球脑会使人类成为“超级英雄”吗? 虚拟现实技术会不会灭绝人类的真实体验? 还有更多不可预知答案的问题,你将在本书中找到属于自己的答案! ➢ 我们的心智正和互联网发生着永无止境的共振,人类思维会因此产生怎样的进化效应?本书编者约翰•布......一起来看看 《人类思维如何与互联网共同进化》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具