XDMicroJSBridge 0.3.0

XDMicroJSBridge 0.3.0

caixindong 维护。



XDMicroJSBridge

CI Status Version License Platform
用于在 UIWebView 中进行 Obj-C 和 JavaScript 通信的最简单 iOS 桥.

示例

要运行示例项目,请克隆仓库,然后首先从 Example 目录中运行 pod install

安装

XDMicroJSBridge 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中

pod 'XDMicroJSBridge'

用法

初始化桥

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

注册方法

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

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

您可以在示例项目中查看详细信息。

作者

caixindong

许可

XDMicroJSBridge可在MIT许可证下使用。更多信息请参阅LICENSE文件。