WVJSBridge is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "WVJSBridge"
// Create `UIWebView` object, DONOT set webView.delegate.
self.webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:_webView];
// Create WVJSBridge
self.bridge = [[WVJSBridge alloc] initWithWebView:_webView bridgeName:@"hnBridge" delegate:self];
// Register native methods for javascript to call
[_bridge registerHandler:@"testObjcCallback" target:self selector:@selector(testObjcCallback:callback:)];
// Call native method with javascript
if(window.hnBridge)
{
ptBridge.testObjcCallback({
// the params to delivery to the native
data:{
param1:"parma1",
param2:"parma2"
},
callback:function(data){
// native callback function
}
});
}
// Or:
if(window.hnBridge)
{
ptBridge.send("testObjcCallback",{
data:{
param1:"parma1",
param2:"parma2"
},
callback:function(data){
// native callback function
}
});
}
- (void)testObjcCallback:(id)data callback:(WVJBResponseCallback)cb
{
NSLog(@"Receive params(`NSDictionary` type) from javascript:%@",[data description]);
if (cb)
{
// Return data to javascript
cb(@{@"code":@(0)});
// Return data to javascript asynchronously
/*
dispatch_async(dispatch_get_global_queue(0, 0), ^{
// TO DO SOMETHINGS
dispatch_async(dispatch_get_main_queue(), ^{
//Return data on main thread
cb(@{@"code":@(0)});
});
});
*/
}
}
hncoder, [email protected]
WVJSBridge is available under the MIT license. See the LICENSE file for more info.
JavaScript bridge between native (iOS) and JavaScript, it modifies and optimizes the WebViewJavascriptBridge.