从iOS7开始 苹果公布了JavaScriptCore.framework 它使得JS与OC的交互更加方便了。首先导入JavaScriptCore.framework
1.选择项目->选择targets->选择General
2.点击Linked Frameworks and Libraries 的添加后 选择 JavaScriptCore.framework
如图

3.把ViewController 设置为咱们的根视图控制器

4.在ViewController中引用,并定义webView为属性

5.初始化Webview

6.给一个url,用webView加载 (在这里我们就用奶茶妹夫的某东就好了,哈哈) 加载失败的看看是不是没有在 info.plist 里面加任意请求HTTP的字段


7.这是webview加载的代理方法

8.在加载成功的方法里,我们首先创建 JSConText对象,通过webView的键获取

9.直接运行,好啦。废话不多说,

大家好,我是大鹏 欢迎大家来我的iOS开发群163619240 ,以后写的Demo 我会发布到群空间里,感谢大家支持。
网友评论