XDMicroJSBridge 0.3.0

XDMicroJSBridge 0.3.0

Maintained by caixindong.



XDMicroJSBridge

CI Status Version License Platform
A most simple iOS bridge for communication between Obj-C and JavaScript in UIWebViews

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Installation

XDMicroJSBridge is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'XDMicroJSBridge'

Usage

Init bridge

#import "XDMicroJSBridge.h"
@property (nonatomic, strong) UIWebView *webview;
@property (nonatomic, strong) XDMicroJSBridge *bridge;
@property (nonatomic, copy) XDMCJSBCallback callback;
self.bridge = [XDMicroJSBridge bridgeForWebView:_webview];

Register methods

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

Call methods in javaScript

<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.

Author

caixindong

License

XDMicroJSBridge is available under the MIT license. See the LICENSE file for more info.