A bridge for sending messages between native iOS and JavaScript in UIWebView/WKWebView.
This library requires iOS 7+ and Xcode 7+. If your project still needs support iOS 6, I’m glad to tell you Cordova and WebViewJavascriptBridge is what you are looking for.
RCBridge supports multiple ways for integrating the library into your awesome project.
Add pod 'RCBridge', '~> 0.1'
in your Podfile
, then run the command pod install
in Terminal.
Add github "looping/RCBridge" ~> 0.1
in your Cartfile
, then run the command carthage
in Terminal to build the framework and drag the built RCBridge.framework
into your Xcode project.
Note: If you hit an error with dyld: Library not loaded: @rpath/RCBridge.framework/RCBridge
during launch app. You should add a New Copy Files Phase
in Build Phases, then add RCBridge.framework
as Frameworks
with Code Sign On Copy
checked.
Just drag the RCBridge
folder into your project. Super easy, wow!
- Import
RCBridge
module:
import RCBridge
Note: If you integrate RCBridge
manually, you should import header file in your Objective-C Bridging Header
file.
- Instantiate RCBridge with a UIWebView or WKWebView;
UIWebView:
let bridge = RCBridge(forWebView: webView)
For WKWebView, you should Instantiate WKWebView with RCBridge.webViewConfiguration()
:
let webView = WKWebView(frame: CGRectZero, configuration: RCBridge.webViewConfiguration())
let bridge = RCBridge(forWebView: webView)
- Add message handler:
bridge.addMethod("you") { (handler) in
}
- Send message back to JavaScript:
let msg: [NSObject : AnyObject] = ["code": 0, "msg": "\(arc4random() % 1024)"]
handler.sendMessageBackToJS(msg)
- Import header file
RCBridge.h
:
import "RCBridge.h"
- Instantiate RCBridge with a UIWebView or WKWebView;
UIWebView:
RCBridge *bridge = [RCBridge bridgeForWebView:webView];
For WKWebView, you should Instantiate WKWebView with RCBridge.webViewConfiguration()
:
WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:[RCBridge webViewConfiguration]];
- Add message handler:
[bridge addMethod:@"you" withHandler:^(RCHandler *handler) {
}];
- Send message back to JavaScript:
[handler sendMessageBackToJS:@{
@"code": @0,
@"msg": [NSString stringWithFormat:@"%@", @(arc4random() % 1024)]
}];
No more imports for F2E guys this time.
- Send message to native iOS:
rcb.send("you", {"got_msg": msg}, function (args) {
})