1.将cordova本地相关的文件copy到已存在的项目中
1.1 打印当前的路径
$ pwd
>>>:"/Users/Mac/Desktop"
1.2 创建一个Cordova目录
$ mkdir Cordova
1.3 进入到Cordova目录
$ cd Cordova
1.4 创建一个cordova项目
$ cordova create hello com.example.hello HelloWorld
>>>:"cordova create hello com.example.hello HelloWorld"
>>>:"Creating a new cordova project."
1.5 进入到hello目录
$ cd hello
1.6 指定生成iOS平台的代码项目
$ 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 进入到"./platforms/ios" 路径
$ cd /Users/Mac/Desktop/Cordova/hello/platforms/ios
1.8 "/Users/Mac/Desktop/Cordova/hello/platforms/ios" 路径下面需要copy的4个文件夹copy到新建项目的根路径中
CordovaLib cordova www platform_www
1.9 需要将下面的两个"config.xml" -> 各自copy到已存在的iOS项目的文件夹内
2.配置相关参数
2.1 下图"Run Script.png" 中点击①中的按钮创建"New Run Script Phase" ,②为创建的"Run Script"
2.2 将下图"Copy www directory.png" 中①原名称"Run Script"修改为"Copy www directory";将②中的选项去掉;将下面的代码复制到③中.
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按照下图配置,然后编译一次程序,最后导入下面两项
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 创建一个"EOPlugin" 类,实现下面的的方法
- EOPlugin.h 实现
import <Cordova/CDVPlugin.h>
@interface EOPlugin : CDVPlugin
- (void)myMethod:(CDVInvokedUrlCommand*)command;
@end
> - EOPlugin.m 实现
> ```
#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", ["回调方法"]);
}
function successFunction(){
alert("successFunction");
}
function failFunction(){
alert("failFunction");
}
</script>
</head>
<body>
<p>ExampleObject</p>
<button onclick="buttonClick()">自定义插件</button>
</body>
</html>
运行效果:
Cordova入门也没有那么难,多看看官方的文档Documentation。自己也练习了下文章在简书上的编写,体验还不错,给个好评。