
//创建cordova工程cordova create cordovaTest com.szcomtop.cordovaTest cordovaTest//进入platforms目录//添加iOS 和 AndroID 平台代码cd platformscordova platform add ios cordova platform add androID2.创建插件//安装插件工具 npm install -g plugman/**创建一个 d出alert的插件 name:插件名称 plugin_ID:插件ID plugin_version:插件版本**/plugman create --name mytoast --plugin_ID com.example.mytoast --plugin_version 0.0.1执行完之后会看见一个mytoast文件夹在根目录下的plugins目录里面。把文件夹名称改成com.example.mytoast2.1 生成package.Json文件//进入到插件目录cd plugins/mytoast//执行创建package.Json文件plugman createpackageJson ./按照默认的选项提示创建即可2.2 创建iOS/AndroID原生代码iOS代码 — AlertPlugin.h 文件#import <Foundation/Foundation.h>#import <Cordova/CDVPlugin.h>NS_ASSUME_NONNulL_BEGIN@interface AlertPlugin : CDVPlugin//接收cordova消息方法- (voID)coolMethod:(CDVInvokedUrlCommand*)command;@endNS_ASSUME_NONNulL_ENDiOS代码 — AlertPlugin.m 文件#import "AlertPlugin.h" @implementation AlertPlugin - (voID)coolMethod:(CDVInvokedUrlCommand*)command{ // NSLog(@"classname:%@ - callbackID:%@ - args:%@ - methodname:%@", // command.classname,command.callbackID,command.arguments,command.methodname); if (command.arguments.count > 0) { UIAlertController *alertCtr = [UIAlertController alertControllerWithTitle:@"显示的标题" message:command.arguments[0] preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:(UIAlertActionStyle)UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsstring:@"OC回传给Js的参数"]; [self.commandDelegate sendpluginResult:result callbackID:command.callbackID]; }]; [alertCtr addAction:okAction]; [[UIApplication sharedApplication].windows.firstObject.rootVIEwController presentVIEwController:alertCtr animated:YES completion:^{ }]; }else{ CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsstring:@"没有参数"]; [self.commandDelegate sendpluginResult:result callbackID:command.callbackID]; } } @endAndroID代码 — mytoast.javapackage com.example.mytoast;import org.apache.cordova.CordovaPlugin;import org.apache.cordova.CallbackContext;import org.Json.JsONArray;import org.Json.JsONException;import org.Json.JsONObject;//导包import androID.Widget.Toast;public class mytoast extends CordovaPlugin { @OverrIDe public boolean execute(String action, JsONArray args, CallbackContext callbackContext) throws JsONException { if (action.equals("coolMethod")) { String message = args.getString(0); this.coolMethod(message, callbackContext); return true; } return false; } private voID coolMethod(String message, CallbackContext callbackContext) { if (message != null && message.length() > 0) { callbackContext.success(message); //吐司内容 Toast.makeText(cordova.getContext(),message, Toast.LENGTH_LONG) .show(); } else { callbackContext.error("Expected one non-empty string argument. "); } }}2.3 修改plugin.xml,添加iOS和AndroID平台相关字段<?xml version='1.0' enCoding='utf-8'?><plugin ID="com.example.mytoast" version="0.0.1" xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:androID="http://schemas.androID.com/apk/res/androID"> <name>mytoast</name> <Js-module name="mytoast" src="www/mytoast.Js"> <clobbers target="cordova.plugins.mytoast" /> </Js-module> <platform name="ios"> <source-file src="src/ios/AlertPlugin.m" /> <header-file src="src/ios/AlertPlugin.h" /> <config-file target="config.xml" parent="/Widget"> <feature name="mytoast"> <param name="ios-package" value="AlertPlugin" /> </feature> </config-file> </platform> <platform name="androID"> <config-file parent="/*" target="res/xml/config.xml"> <feature name="mytoast"> <param name="androID-package" value="com.example.mytoast.mytoast"/> </feature> </config-file> <config-file parent="/*" target="AndroIDManifest.xml"></config-file> <source-file src="src/androID/mytoast.java" target-dir="src/com/example/mytoast/mytoast"/> </platform></plugin>2.4 插件最终的目录结构com.example.mytoast |-- package.Json |-- plugin.xml |-- src |-- ios |-- AlertPlugin.h |-- AlertPlugin.m |-- androID |-- mytoast.java |-- www |-- mytoast.Js按照上面的目录结构,把原生文件放到对应的位置。3. 测试插件3.1 执行cordova命令添加插件//add 后面参数为 我们新创建的插件文件夹cordova plugin add ./plugins/com.example.mytoast3.2 编写测试index.HTML<!DOCTYPE HTML><HTML> <head> <Title>TestPlugin</Title> <Meta http-equiv="Content-type" content="text/HTML; charset=utf-8"> <script type="text/JavaScript" charset="utf-8" src="cordova.Js"></script> <script type="text/JavaScript" charset="utf-8"> function testPlugin() { cordova.plugins.mytoast.coolMethod("我是Js传来的参数!",alertSuccess,alertFail); } function alertSuccess(msg) { alert(msg); } function alertFail(msg) { alert('调用OC失败: ' + msg); } </script> </head> <body > <button onclick="testPlugin();">调用iOS插件</button> <br> </body></HTML>3.3 执行效果Demo插件附件插件Demo下载
官网参考地址cordova的plugin.xml字段说明
总结以上是内存溢出为你收集整理的Cordova插件开发(iOS/Android)--看这篇就够了全部内容,希望文章能够帮你解决Cordova插件开发(iOS/Android)--看这篇就够了所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)