/*
************************************************** 关于WebViewJavascriptBridge ******************************************
* 本地主动发消息给服务器(本地被动行为):
* 服务器注册:
bridge.registerHandler('testJavascriptHandler', function(data, responseCallback) {
log('ObjC called testJavascriptHandler with', data)
var responseData = { 'Javascript Says':'Right back atcha!' }
log('JS responding with', responseData)
responseCallback(responseData)
})
* 需要本地发送:
[_bridge callHandler:@"testJavascriptHandler" data:@{ @"foo":@"before ready" }];
或者
[_bridge callHandler:@"testJavascriptHandler" data:data responseCallback:^(id response) {
NSLog(@"testJavascriptHandler responded: %@", response);
}];
**************************************************************************************************************************
* 服务器向本地发消息(服务器被动行为):
* 本地注册:
[_bridge registerHandler:@"testObjcCallback" handler:^(id data, WVJBResponseCallback responseCallback) {
NSLog(@"testObjcCallback called: %@", data);
responseCallback(@"Response from testObjcCallback");
}];
* 服务器发消息:
bridge.callHandler('testObjcCallback', {'foo': 'bar'}, function(response) {
log('JS got response', response)
})
**************************************************************************************************************************
* 服务器发送消息(服务器主动行为):
bridge.send(data, function(responseData) {
log('JS got response', responseData)
})
* 会响应本地注册_bridge的Handler:
_bridge = [WebViewJavascriptBridge bridgeForWebView:webView webViewDelegate:self handler:^(id data, WVJBResponseCallback responseCallback) {
NSLog(@"ObjC received message from JS: %@", data);
responseCallback(@"Response for message from ObjC");
}];
**************************************************************************************************************************
* 本地发送消息(本地主动行为):
[_bridge send:@"A string sent from ObjC before Webview has loaded." responseCallback:^(id responseData) {
NSLog(@"objc got response! %@", responseData);
}];
或者
[_bridge send:@"A string sent from ObjC after Webview has loaded."];
* 服务器会响应服务器注册_bridge时的Handler:
bridge.init(function(message, responseCallback) {
log('JS got a message', message)
var data = { 'Javascript Responds':'Wee!' }
log('JS responding with', data)
responseCallback(data)
})
**************************************************************************************************************************/
关于WebViewJavascriptBridge
最新推荐文章于 2023-06-15 11:32:50 发布