A most simple iOS bridge for communication between Obj-C and JavaScript
To run the example project, clone the repo, and run pod install
from the Example directory first.
XDMicroJSBridge is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'XDMicroJSBridge'
//if you use UIWebView
#import "XDMicroJSBridge.h"
@property (nonatomic, strong) UIWebView *webview;
@property (nonatomic, strong) XDMicroJSBridge *bridge;
@property (nonatomic, copy) XDMCJSBCallback callback;
self.bridge = [XDMicroJSBridge bridgeForWebView:_webview];
//////////////////////////////////////////////////////////
//if you use WKWebView
#import "XDMicroJSBridge_WK.h"
@property (nonatomic, strong) WKWebView *webView;
@property (nonatomic, strong) XDMicroJSBridge_WK *bridge;
@property (nonatomic, copy) XDMCJSBCallback callback;
self.bridge = [[XDMicroJSBridge_WK alloc] init];
////important tips
self.webView = [_bridge getBridgeWebView];
//设置命名空间,注意顺序,必须在getBridgeWebView之后调用才能生效
_bridge.nameSpace = @"xxxxx";
__weak typeof(self) weakself = self;
[_bridge registerAction:@"camerapicker" handler:^(NSArray *params, XDMCJSBCallback callback) {
dispatch_async(dispatch_get_main_queue(), ^{
//if your javaScript method has callback, you should register this call like this.
if (callback) {
weakself.callback = callback;
}
UIImagePickerController *cameraVC = [[UIImagePickerController alloc] init];
cameraVC.delegate = weakself;
cameraVC.sourceType = UIImagePickerControllerSourceTypeCamera;
[weakself presentViewController:cameraVC animated:YES completion:nil];
});
}];
<script>
function clickcamera() {
XDMCBridge.camerapicker(function (response) {
var photos = response['photos'];
var insert = document.getElementById('insert');
for(var i = 0; i < photos.length; i++) {
var img = new Image(100,100);
img.src = photos[i];
insert.appendChild(img);
}
});
}
</script>
You can see the details in demo project.
caixindong
XDMicroJSBridge is available under the MIT license. See the LICENSE file for more info.