赞
踩
@objc protocol JavaScriptSwiftDelegate: JSExport {
func method(str:String) //带参数的
func method2()//不带参数
}
@objc class SwiftJavaScriptModel: NSObject, JavaScriptSwiftDelegate {
weak var jsContext: JSContext?
typealias customeBlock = (str: String )->Void
typealias customeBlock2 = ()->Void
var block:customeBlock?
var block2:customeBlock2?
func method(str: String) {
block(str:str)
}
func method2(){
block2()
}
}
yourweb.delegate = self
let context=webView.valueForKeyPath("documentView.webView.mainFrame.javaScriptContext") as? JSContext
let model = SwiftJavaScriptModel()
model.controller = self
model.jsContext = context
model.block = {
//实现对应的操作 调用swift的方法
}
self.jsContext = context
//下面的方法是把model 注入js里面 native是js里面方法的前缀 比如js里面的方法是
//native.method()
self.jsContext?.setObject(model, forKeyedSubscript: "native")
let url = NSBundle.mainBundle().URLForResource("html路径", withExtension: "html")
self.jsContext.evaluateScript(try?String.init(contentsOfURL: url!))
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。