Latest0.3.0
Homepagehttps://github.com/caixindong/XDMicroJSBridge
LicenseMIT
Platformsios 8.0
FrameworksUIKit, JavaScriptCore, WebKit
Authors

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

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

//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];

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.3.0",
    "summary": "A most simple iOS bridge for communication between Obj-C and JavaScript",
    "description": "A most simple iOS bridge for communication between Obj-C and JavaScript",
    "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.3.0"
    },
    "platforms": {
        "ios": "8.0"
    },
    "source_files": "XDMicroJSBridge/Classes/**/*",
    "public_header_files": "XDMicroJSBridge/Classes/**/*.h",
    "frameworks": [
        "UIKit",
        "JavaScriptCore",
        "WebKit"
    ]
}

Pin It on Pinterest

Share This