Skip to content

More simple more light more easy to use for iOS/OSX bridge with Javascript. Also easy to get js console.log in Xcode.

License

Notifications You must be signed in to change notification settings

zickc/JavascriptBridge

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WebViewJavascriptBridge

An iOS/OSX bridge for sending messages between Obj-C and JavaScript in WKWebViews. Also easy to get js console.log.

More simple more light. Refactor WebViewJavascriptBridge with AOP

How to use ?

Installation with CocoaPods

Add this to your podfile and run pod install to install:

pod 'SKJavaScriptBridge', '~> 1.0.3'

If you can't find the last version, maybe you need to update local pod repo.

pod repo update

Manual installation

Drag the WebViewJavascriptBridge folder into your project.

In the dialog that appears, uncheck "Copy items into destination group's folder" and select "Create groups for any folders".

Usage

  1. Import the header file and declare an ivar property:
#import "WebViewJavascriptBridge.h"
@property (nonatomic, strong) WKWebView *webView;
@property (nonatomic, strong) WebViewJavascriptBridge* bridge;
    self.webView = [[WKWebView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:self.webView];
    if(!_bridge){
              _bridge = [WebViewJavascriptBridge bridgeForWebView:self.webView
              showJSconsole:YES
              enableLogging:YES];
       }
  1. Register a handler in ObjC, and call a JS handler:
[_bridge registerHandler:@"ObjC Echo" handler:^(id data, WVJBResponseCallback responseCallback) {
	NSLog(@"ObjC Echo called with: %@", data);
	responseCallback(data);
}];
[_bridge callHandler:@"JS Echo" data:nil responseCallback:^(id responseData) {
	NSLog(@"ObjC received response: %@", responseData);
}];
  1. Copy and paste setupWebViewJavascriptBridge into your JS:
function setupWebViewJavascriptBridge(callback) {
	 return callback(WebViewJavascriptBridge); 
}
  1. Finally, call setupWebViewJavascriptBridge and then use the bridge to register handlers and call ObjC handlers:
setupWebViewJavascriptBridge(function(bridge) {
	
	/* Initialize your app here */

	bridge.registerHandler('JS Echo', function(data, responseCallback) {
		console.log("JS Echo called with:", data)
		responseCallback(data)
	})
	bridge.callHandler('ObjC Echo', {'key':'value'}, function responseCallback(responseData) {
		console.log("JS received response:", responseData)
	})
})

Any question.

English video https://www.youtube.com/watch?v=2Z3852I58OQ.
Also you can contact me with:[email protected]

如果你有疑问.

你可以观看中文版的视频介绍:https://www.youtube.com/watch?v=ocyKSpMT7tM
当然你也可以邮件联系我:[email protected]

About

More simple more light more easy to use for iOS/OSX bridge with Javascript. Also easy to get js console.log in Xcode.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Objective-C 71.8%
  • Ruby 19.0%
  • HTML 9.2%