1.將cordova本地相關(guān)的文件copy到已存在的項(xiàng)目中
1.1 打印當(dāng)前的路徑
$ pwd
>>>:"/Users/Mac/Desktop"
1.2 創(chuàng)建一個(gè)Cordova目錄
$ mkdir Cordova
1.3 進(jìn)入到Cordova目錄
$ cd Cordova
1.4 創(chuàng)建一個(gè)cordova項(xiàng)目
$ cordova create hello com.example.hello HelloWorld
>>>:"cordova create hello com.example.hello HelloWorld"
>>>:"Creating a new cordova project."
1.5 進(jìn)入到hello目錄
$ cd hello
1.6 指定生成iOS平臺(tái)的代碼項(xiàng)目
$ cordova platform add ios
>>>:"Adding ios project..."
>>>:"iOS project created with cordova-ios@4.0.1"
>>>:"Discovered plugin "cordova-plugin-whitelist" in config.xml. Installing to the project"
>>>:"Fetching plugin "cordova-plugin-whitelist@1" via npm"
>>>:"Installing "cordova-plugin-whitelist" for ios"
1.7 進(jìn)入到"./platforms/ios" 路徑
$ cd /Users/Mac/Desktop/Cordova/hello/platforms/ios
1.8 "/Users/Mac/Desktop/Cordova/hello/platforms/ios" 路徑下面需要copy的4個(gè)文件夾copy到新建項(xiàng)目的根路徑中
CordovaLib cordova www platform_www
1.9 需要將下面的兩個(gè)"config.xml" -> 各自copy到已存在的iOS項(xiàng)目的文件夾內(nèi)
2.配置相關(guān)參數(shù)
2.1 下圖"Run Script.png" 中點(diǎn)擊①中的按鈕創(chuàng)建"New Run Script Phase" 拴疤,②為創(chuàng)建的"Run Script"
2.2 將下圖"Copy www directory.png" 中①原名稱"Run Script"修改為"Copy www directory"永部;將②中的選項(xiàng)去掉;將下面的代碼復(fù)制到③中.
NODEJS_PATH=/usr/local/bin; NVM_NODE_PATH=~/.nvm/versions/node/`nvm version 2>/dev/null`/bin; N_NODE_PATH=`find /usr/local/n/versions/node/* -maxdepth 0 -type d 2>/dev/null | tail -1`/bin; XCODE_NODE_PATH=`xcode-select --print-path`/usr/share/xcs/Node/bin; PATH=$NODEJS_PATH:$NVM_NODE_PATH:$N_NODE_PATH:$XCODE_NODE_PATH:$PATH && node cordova/lib/copy-www-build-step.js
2.3 Build Settings -> Other Linker Flags 中添加"-ObjC -all_load"
2.4 Add Files to ... -> CordovaLib.xcodeproj
2.5 Add Files to ... -> config.xml
2.6 Add Files to ... -> www文件夾
2.7 Schemes按照下圖配置呐矾,然后編譯一次程序苔埋,最后導(dǎo)入下面兩項(xiàng)
Build Phases -> Target Dependencies -> CordovaLib
Build Phases -> Link Binary With Libraries -> libCordova.a
2.8 Schemes按照下圖配置,將"ViewController.h" 文件改為
#import <Cordova/CDVViewController.h>
#import <Cordova/CDVCommandDelegateImpl.h>
#import <Cordova/CDVCommandQueue.h>
@interface ViewController : CDVViewController
@end
3. 自定義插件
3.1 在config.xml文件中加入
<feature name="YourPluginName">
<param name="ios-package" value="EOPlugin" />
<param name="onload" value="true" />
</feature>
3.2 創(chuàng)建一個(gè)"EOPlugin" 類蜒犯,實(shí)現(xiàn)下面的的方法
- EOPlugin.h 實(shí)現(xiàn)
import <Cordova/CDVPlugin.h>
@interface EOPlugin : CDVPlugin
- (void)myMethod:(CDVInvokedUrlCommand*)command;
@end
> - EOPlugin.m 實(shí)現(xiàn)
> ```
#import "EOPlugin.h"
@implementation EOPlugin
- (void)myMethod:(CDVInvokedUrlCommand*)command
{
CDVPluginResult* pluginResult = nil;
NSString* myarg = [command.arguments objectAtIndex:0];
if (myarg != nil) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Arg was null"];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
@end
3.3 編寫index.html文件如下
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="cordova_plugins.js"></script>
<script type="text/javascript">
document.addEventListener("deviceready", yourCallbackFunction, false);
function buttonClick(){
Cordova.exec(successFunction, failFunction, "YourPluginName", "myMethod", ["回調(diào)方法"]);
}
function successFunction(){
alert("successFunction");
}
function failFunction(){
alert("failFunction");
}
</script>
</head>
<body>
<p>ExampleObject</p>
<button onclick="buttonClick()">自定義插件</button>
</body>
</html>
運(yùn)行效果:
Cordova入門也沒(méi)有那么難组橄,多看看官方的文檔Documentation。自己也練習(xí)了下文章在簡(jiǎn)書上的編寫罚随,體驗(yàn)還不錯(cuò)玉工,給個(gè)好評(píng)。