印言
最近接到一个需求:监听网页的按钮的点击事件,并且网页不是我们招呼一声对方就能改的。那么一切的故事从这里开始了。正文部分主要围绕监听网页的事件为主线。
正文
WKWebView加载本地html页面,
搭建UI做准备工作。
NSString *path = [[NSBundle mainBundle] pathForResource:@"oc&js.html" ofType:nil];
[_wkWeb loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]];
html和oc各创建两个按钮,俩个按钮分别测试无参数和有参数消息的传递。
<h2>是H5按钮</h2>
<button type="button" onclick="js2oc1()">js调用oc的按钮1</button>
<button type="button" onclick="js2oc2('Gavin','18')">js调用oc的按钮2</button>
<!-- 用于显示oc点击事件的结果 -->
<h1 id=labelId>label</h1>
// OC的按钮
_button1 = [[UIButton alloc] initWithFrame:CGRectMake(100, self.view.bounds.size