cordova创建iOS项目

1.配置环境
1. cordova是利用nodeJS进行管理的, 首先下载nodeJS (http://nodejs.org)
2. 使用命令行安装
sudo npm install -g cordova
3. 创建工程
cordova create hello(文件夹名) com.example.hello(id) HelloWorld(工程名)
4.添加相关组件并运行
cd hello
cordova platform add ios
cordova build
2.写插件
iOS类, 放入目录Plugins下

#import <Cordova/CDVPlugin.h>

@interface Test2 : CDVPlugin

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

@end
#import "Test2.h"

@implementation Test2

-(void)test:(CDVInvokedUrlCommand *)command
{
    CDVPluginResult * pluginResult = nil;
    NSString* echo = [command.arguments objectAtIndex:0];

    if (echo != nil && [echo length] > 0) {
        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"ios send OK"]; 
    } else {
        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
    }

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

}

@end

3.Login.html文件加入工程

    路径: hello->platforms->ios->www下(工程中Staging下www)

4.login,js文件加入工程

    路径: hello->platforms->ios->www->plugins->tansun-plugin-login->www下(路径不存在就新建)

5.文件配置
config.xml中添加

<feature name="Login">
        <param name="ios-package" value="Test2" />
        <param name="onload" value="true" />
        </feature>
其中Login为自定义名字 Test2为插件类名

cordova_plugins.js中添加
        {
            "file": "plugins/tansun-plugin-login/www/login.js",//login.js路径
            "id": "com.gldjc.guangcaiclient.login",//js文件定义的id
            "clobbers": [
                  "Login"//config.xml中自定义的名字
            ]

        }

6.使用
继承类

#import <Cordova/CDVViewController.h>
    @interface MainViewController : CDVViewController
重写init方法
    设置self.startPage = @"Login.html";

7.通讯
OC主动调JS

        id <CDVWebViewEngineProtocol> engine = self.webViewEngine;
    if(engine) {
        [engine evaluateJavaScript:[NSString stringWithFormat:@"方法名('%@','%@')", self.name, self.password] completionHandler:^(id result, NSError *error) {
            NSLog(@"result == %@, error == %@", error);
        }];
    }
JS通过cordova调OC
        function mybtn(){
        Login.loginSuccess(); //Login为自定义的名字
        }

        loginSuccess:function() {
        exec(function(PluginResult){
            alert(PluginResult);
        }, null, "Login", "test", ["AAAAA","BBBBB"]);
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值