如果還沒有配置過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"]);
}
});