美文网首页
Cordova 插件开发

Cordova 插件开发

作者: Alfred的记录本 | 来源:发表于2017-08-31 11:27 被阅读0次

    cordova 插件 (最简单,步骤最少的实现)

    1、 JS调用AlfreldPlugin 为暴露出来的名称,myMethod为方法名。

    // index.js
    Cordova.exec(successFunction, failFunction, "AlfreldPlugin", "myMethod", ["function"]);
    

    2、 config.xml,配置AlfreldPlugin 到 TestPlugin的映射,

    <feature name="AlfreldPlugin">
          <param name="ios-package" value="TestPlugin" />
          <param name="onload" value="true" />
    </feature>
    

    3、原生实现

    //TestPlugin.h
    @interface TestPlugin : CDVPlugin
      - (void)myMethod:(CDVInvokedUrlCommand*)command;
    @end
    
      //TestPlugin.m
    @implementation TestPlugin
    -(void) pluginInitialize {
        return;
    }
    - (void)myMethod:(CDVInvokedUrlCommand*)command
    {
        CDVPluginResult* pluginResult = nil;
        NSString* myarg = [command.arguments objectAtIndex:0];//获取第一个参数
        if (myarg != nil) {
            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"return Data  :) "];
        } else {
            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Arg was null"];
        }
        [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
    }
    @end
    

    cordova 插件 (标准实现)

    1、barcodescanner.js 中实现调用,注意BarcodeScanner为config.xml中的BarcodeScanner

    cordova.define("phonegap-plugin-barcodescanner.BarcodeScanner", function(require, exports, module) {
            var exec = cordova.require("cordova/exec");
            var scanInProgress = false;
            function BarcodeScanner() {//......};
             //......
             BarcodeScanner.prototype.scan = function (successCallback, errorCallback, config) {
                exec(
                    function(result) {
                        scanInProgress = false;
                        successCallback(result);
                    },
                    function(error) {
                        scanInProgress = false;
                        errorCallback(error);
                    },
                    'BarcodeScanner',
                    'scan',
                    config
                );
            };
             //......
            var barcodeScanner = new BarcodeScanner();
            module.exports = barcodeScanner;
    });
    

    2、cordova_plugin.js实现对插件的配置,注意:该配置中id为1中define的内容,pluginId为module.exports.metadata的内容,file指向barcodescaner.js,clobbers为调用插件时的引用。

    {
            "id": "phonegap-plugin-barcodescanner.BarcodeScanner",
            "file": "plugins/phonegap-plugin-barcodescanner/www/barcodescanner.js",
            "pluginId": "phonegap-plugin-barcodescanner",
            "clobbers": [
                "cordova.plugins.barcodeScanner"
            ]
        },
    //...
    module.exports.metadata = 
    {
        "phonegap-plugin-barcodescanner": "6.0.6",
    };
    

    3、config.xml,CDVBarcodeScaner为实际的类名

        <feature name="BarcodeScanner">
            <param name="ios-package" value="CDVBarcodeScanner" />
        </feature>
    

    4、原生实现
    5、js调用,注意cordova.plugins.barcodeScanner跟步骤2中的clobbers一致。

    cordova.plugins.barcodeScanner.scan(
          function (result) {
              alert("We got a barcode\n" +
                    "Result: " + result.text + "\n" +
                    "Format: " + result.format + "\n" +
                    "Cancelled: " + result.cancelled);
          },
          function (error) {
              alert("Scanning failed: " + error);
          }
       );
    

    插件开发时的注意事项

    1、当下面参数设置为 YES 可以设置多次跟js进行交互
    - (void)setKeepCallbackAsBool:(BOOL)bKeepCallback;
    2、CDVPlugin中可以通过变量viewController获取当前页面所在的viewController

    [self.viewController presentViewController:_readerViewController animated:YES completion:nil];
    

    相关文章

      网友评论

          本文标题:Cordova 插件开发

          本文链接:https://www.haomeiwen.com/subject/oajpcxtx.html