Skip to content

A bridge for sending messages between native iOS and JavaScript in UIWebView/WKWebView.

License

Notifications You must be signed in to change notification settings

looping/RCBridge

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RCBridge

A bridge for sending messages between native iOS and JavaScript in UIWebView/WKWebView.

Requirements

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.

Installation

RCBridge supports multiple ways for integrating the library into your awesome project.

CocoaPods

Add pod 'RCBridge', '~> 0.1' in your Podfile, then run the command pod install in Terminal.

Carthage

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.

Manual

Just drag the RCBridge folder into your project. Super easy, wow!

Usage

Swift

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

Objective-C

  • 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)]
	}];

JavaScript

No more imports for F2E guys this time.

  • Send message to native iOS:
	rcb.send("you", {"got_msg": msg}, function (args) {
	})

About

A bridge for sending messages between native iOS and JavaScript in UIWebView/WKWebView.

Resources

License

Stars

Watchers

Forks

Packages

No packages published