美文网首页
(Cordova)使用plugman自定义插件

(Cordova)使用plugman自定义插件

作者: 布呐呐u | 来源:发表于2022-05-04 21:28 被阅读0次

    一)安装plugman

    npm install -g plugman
    

    二)使用plugman

    • cd至任意目录,本文在桌面执行指令
    • --name 插件名称 (TestPlugin)
    • --plugin_id 插件id (cordova-plugin-testplugin)
    • --plugin_version 插件版本号 (0.0.1)
    plugman create --name TestPlugin --plugin_id cordova-plugin-testplugin --plugin_version 0.0.1
    

    三)添加插件支持的平台

    • cd至插件根目录,本文以iOS平台为例
    plugman platform add --platform_name ios
    

    四)创建package.json文件

    • cd至插件根目录,执行npm init指令,生成package.json文件
    npm init
    
    • name 推荐使用 plugin_id
    • version 版本号,回车 即默认 plugin_version
    • description 描述,回车 即默认 空字符串
    • main 入口文件,回车 即默认 index.js
    • scripts 脚本,回车 即默认 "test": "echo "Error: no test specified" && exit 1"
    • author 作者,回车 即默认 空字符串
    • license 许可证,回车 即默认 ISC
      "name": "cordova-plugin-photobrower",
      "version": "0.0.1",
      "description": "miaos",
      "main": "index.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "author": "",
      "license": "ISC"
    

    五)修改配置plugin.xml文件

    <?xml version="1.0" encoding="utf-8"?>
    <plugin id="cordova-plugin-testplugin" version="0.0.1" xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android">
        <name>TestPlugin</name> 
            <!-- src="www/TestPlugin.js",JavaScript资源路径 -->
        <js-module name="TestPlugin" src="www/TestPlugin.js">
            <!-- 可以自定义,调用时 TestPlugin.coolMethod -->
            <clobbers target="TestPlugin" />
        </js-module>
        <platform name="ios">
            <config-file parent="/*" target="config.xml">
                <!-- name="TestPlugin",与www/TestPlugin.js 文件中调用名称'TestPlugin'一致 -->
                <feature name="TestPlugin">
                    <!-- value="TestPlugin",iOS类名 -->
                    <param name="ios-package" value="TestPlugin" />
                </feature>
            </config-file>
            <!-- src="src/ios/TestPlugin.m",iOS资源路径 -->
            <source-file src="src/ios/TestPlugin.m" />
        </platform>
    </plugin>
    

    六)编写插件

    • TestPlugin.js
    var exec = require('cordova/exec');
    
    exports.coolMethod = function (arg0, success, error) {
        exec(success, error, 'TestPlugin', 'coolMethod', [arg0]);
    };
    
    • TestPlugin.m
    - (void)coolMethod:(CDVInvokedUrlCommand*)command
    {
        CDVPluginResult* pluginResult = nil;
        NSString* echo = [command.arguments objectAtIndex:0];
    
        if (echo != nil && [echo length] > 0) {
            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:echo];
        } else {
            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
        }
    
        [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
    }
    

    七)安装插件

    • cd至cordova项目根目录
    • 执行如下指令
    cordova plugin add /Users/XXXX/Desktop/TestPlugin
    

    八)使用插件

    TestPlugin.coolMethod('一个测试字符串')
    

    相关文章

      网友评论

          本文标题:(Cordova)使用plugman自定义插件

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