Latest 0.2.3
Homepage https://github.com/caixindong/XDMicroJSBridge
License MIT
Platforms ios 8.0
Frameworks UIKit, JavaScriptCore
Authors

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.

Latest podspec

{
    "name": "XDMicroJSBridge",
    "version": "0.2.3",
    "summary": "A short description of XDMicroJSBridge.",
    "description": "TODO: Add long description of the pod here.",
    "homepage": "https://github.com/caixindong/XDMicroJSBridge",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "[email protected]": "[email protected]"
    },
    "source": {
        "git": "https://github.com/caixindong/XDMicroJSBridge.git",
        "tag": "0.2.3"
    },
    "platforms": {
        "ios": "8.0"
    },
    "source_files": "XDMicroJSBridge/Classes/**/*",
    "public_header_files": "XDMicroJSBridge/Classes/**/*.h",
    "frameworks": [
        "UIKit",
        "JavaScriptCore"
    ]
}

Pin It on Pinterest

Share This