开发自己的cordova插件

如果还没有配置过cordova环境,首先要下载nodejs,(下载地址https://nodejs.org/)下载完毕安装。

控制台:

1.输入npm -v 确定是否装上了

2.输入sudo npm install -g cordova安装cordova

3.等待几分钟,输入cordova -v查看是否安装成功

4.输入sudo npm install -g ionic 安装ionic

到这里cordova环境已经配置完毕。接下来我们来创建一个工程

控制台输入 ionic start testDemo,进入testDemo会发现里面有好多文件夹,这里面platforms代表支持的平台,plugins是添加的插件。

控制台进入testDemo目录下面,输入命令:cordova platform add ios。意思是添加ios平台。进入ios目录下打开testDemo.xcodeproj。接着就可以编写你想要的插件了,举个例子,编写弹框的demo。

新建一个文件TestAlert继承于CDVPlugin

 

注意将.h文件中的导入的头文件改成CDVPlugin.h不然会报错的。这是cordova本身提示的缺陷

#import <Cordova/CDVPlugin.h>

 

@interface TestAlert : CDVPlugin

-(void)showAlert:(CDVInvokedUrlCommand *)command;

@end

 

.m文件中实现

-(void)showAlert:(CDVInvokedUrlCommand *)command{

  

    CDVPluginResult* pluginResult = nil;

    NSString *alertTitle = command.arguments[0];

    NSString *alertMessage = command.arguments[1];

    NSString *alertCalBtn = command.arguments[2];

    NSString *alertOthBtn = command.arguments[3];

    

    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:alertTitle message:alertMessage delegate:self cancelButtonTitle:alertCalBtn otherButtonTitles:alertOthBtn, nil];

    [alert show];

    NSString *vakc = @"返回值成功是调用此方法";

    if ((alertTitle != nil || alertMessage != nil || alertCalBtn != nil || alertOthBtn != nil) && ([alertTitle length] > 0 || [alertMessage length]>0 ||[alertCalBtn length]>0 || [alertOthBtn length]>0)) {

        //返回成功,messageAsString将数据返回到JavaScript。

        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:vakc];

    } else {

        //返回失败。

        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];

    }

    //将结果发送给<code>self.commandDelegate</code>,这样会执行JavaScript side的成功或失败方法。

    [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];

}

 到这里原生代码的书写已经完毕,接下来就是配置文件了

找到config.xml文件,配置如下信息

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0.0"

    id="cordova-plugin-diyalert"

    version="1.0.0">

    <!--name:插件的名称-->

    <name>DIYAlert</name>

    <!--description:描述信息-->

    <description>

        This plugin allows you to DIY alert.

    </description>

    

    <!--  js-module:对应我们的 javascript 文件,src 属性指向 www/xxx.js路径-->

    <js-module src="www/zzAlert.js" name="TestAlert">

        <clobbers target="TestAlert" />

    </js-module>

    

    <!-- platform:支持的平台ios -->

    <platform name="ios">

        <config-file target="config.xml" parent="/*">

            <feature name="TestAlert">

                <param name="ios-package" value="TestAlert"/>

            </feature>

        </config-file>

        

        <header-file src="src/ios/TestAlert.h"/>

        <source-file src="src/ios/TestAlert.m"/>

    </platform>  

</plugin>

然后在js文件中找到.js文件,搭建原生和js的通道。

var exec = require('cordova/exec');
module.exports = {
	showAlert:function(successCallback,errorCallback,arg1,arg2,arg3,arg4){
		exec(successCallback, errorCallback, "TestAlert", "showAlert", 			[arg1,arg2,arg3,arg4]);
}
}

function()里前两个参数分别代表调用成功,调用失败。“TestAlert”是你的原生文件名字,"showAlert"是你自己定义的原生方法。后面的都是回传的参数,也可以是空的。这里看你有没有需要了。

到这里插件已经写好了。那么你就可以拿来打包给别人用了。

打包流程:

1.在新建一个目录,此目录下再新建www,src两个文件夹,以及一个plugin.xml文件,它的内容就是刚才配置好的config.xml内容。然后到src文件夹下新建一个ios文件夹,将刚才写的原生文件拷贝到ios文件夹下(这里是TestAlert.h/TestAlert.m)然后回到www文件下,将.js文件拷贝进去。注意:这里的.js文件的名字要与你的.xml文件中的js文件名字一样。

2.接下来就是将它到进你的工程进去就好。控制台:cordova plugin add .....(你的插件路径)

3.在进入你的工程里面,你会发现Plugins自动添加了TestAlert.h和TestAlert.m问价,config.xml文件中自动帮你配置好了

<feature name="TestAlert">

        <param name="ios-package" value="TestAlert" />

    </feature>

4.最后就是js调用你的原生代码了,js中编写如下代码

angular.module('starter',[])

.controller('myCtrl',function($scope) {

            $scope.aaa = function(){

            cordova.exec(function(s){

                         console.log(s)

                         }, function(err) {

                         console.log(err);

                         }, "TestAlert", "showAlert", ["提示","这是自定制的弹框","知道了","OK"]);

            }

            

            });

 

 

posted @ 2016-05-06 14:46  V清风  阅读(2416)  评论(0编辑  收藏  举报