Coder Social home page Coder Social logo

housenkui / javascriptbridge Goto Github PK

View Code? Open in Web Editor NEW
85.0 4.0 11.0 92 KB

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

License: MIT License

Objective-C 71.86% Ruby 18.96% HTML 9.18%

javascriptbridge's Introduction

Please Jump 请使用这个库,它更加完善 to here.

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

javascriptbridge's People

Contributors

housenkui avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

javascriptbridge's Issues

有个BUG,不知道该怎么解决

目前用的WKWebView,在调用:[_bridge callHandler:@"replaceimage%@,%@" data:[self replaceUrlSpecialString:info.src] responseCallback:^(id responseData) {
NSLog(@"ObjC received response: %@", responseData);
}];
这句话的时候,block并不会回调,也就是说这句话没有执行成功,换到我的项目里就是图片不能加载出来,而如果使用webView的话,就可以正常加载出图片,block也会回调,这个问题不止在你这里出现,在WebViewJavascriptBridge这个框架里也有这个问题

WKWebView Not getting dealloc'ed

Hi,

A great library for logging purposes.
There is one issue where if the logs are enabled then it holds onto the webview's instance which leads to memory leaks.

使用问题

改用你这个库,之前WebViewJavascriptBridge这个的回调全部没有了,你这库和这个改动了什么

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.