首先向大家推荐一篇文章关于iOS与js交互知识点的总结,个人认为有助于大家记忆和理解,感谢作者的整理。
https://github.com/Haley-Wong/JS_OC
关于OC与JS交互方法,我主要使用的是WKWebview,所以js调用oc方法并传值,oc调用js方法并传值,我使用的是MessageHandler。JS_OC_MessageHandler是利用WKWebView提供的新的API实现的JS调用原生OC,更简洁和方便。
一、js调用oc方法:
首先在viewdidload里,配置WKUserContentController和配置WKWebView,代码如下:
//js调用iOS方法配置
WKUserContentController *userContentController = [[WKUserContentController alloc] init];
WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
configuration.us