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);
 

posted on 2012-11-15 17:25  无量少年  阅读(1706)  评论(0编辑  收藏  举报

导航