webview与js的通信框架
代码开源在:https://github.com/ohsc/TGJSBridge
摘自:http://www.cocoachina.com/bbs/read.php?tid=113015&keyword=webview
OC中初始化:
1
2
|
TGJSBridge *jsBridge = [TGJSBridge jsBridgeWithDelegate: webViewDelegate]; <u><font color= "\"red\"" >webView</font></u>.delegate = jsBridge; |
发消息到JS:
1
|
postNotificationName:userInfo:toWebView: |
监听消息:
1
|
- ( void )jsBridge:(TGJSBridge *)bridge didReceivedNotificationName:( NSString *)name userInfo:( NSDictionary *)userInfo fromWebView:(UIWebView *)<u><font color= "\"red\"" >webview</font></u> |
js发送消息:
1
|
jsBridge.postNotification(msgName,userInfo); |
js监听消息:
1
2
3
|
jsBridge.bind(msgName, function(userInfo){ ... }); |
js取消监听消息:
1
|
jsBridge.unbind( 'test' ,callbackHandler); |