现在的位置: 首页 > 综合 > 正文

Cordova IOS 的使用

2017年10月30日 ⁄ 综合 ⁄ 共 3795字 ⁄ 字号 评论关闭


使用环境:mac 10.8 xcode 4.6
首先新建一个工程CordovaExample,安装通过命令提示行安装Node.js和Cordova
安装方法参照:http://cordova.apache.org/docs/en/3.5.0//guide_cli_index.md.html#The%20Command-Line%20Interface

安装完成以后进入刚刚创建的CordovaExample目录,创建App(参考:http://cordova.apache.org/docs/en/3.5.0//guide_cli_index.md.html#The%20Command-Line%20Interface)
命令:cordova create shopmall com.example.mall mall
创建完成以后,我们添加平台支持,Cordova支持多种平台,有(windows,android,ios等)在这里我们添加IOS平台
进入刚刚创建的shopmall目录,
命令:cd shopmall
添加平台命令:cordova platform add ios
添加完成以后编译,在每次修改了www目录下面的内容以后都需要重新,编译命令如下
cordova build
如果我们想要访问设设备,我们就需要引入Plugin Features,在这里由于我们不执行任何操作只是让html页面能够显示出来,所以我们这个不在引入Cordova api
引入api参考网址同上
选中新建的CordovaExample,右键点击,选择"New Group",如下图,

输入名称mall,完成以后选中刚刚创建的new group,右键点击 选择添加文件到工程 选择我们刚刚创建的shopmall->platforms->ios-mall工程文件,如下图:

添加完成以后如下图:

接下来我们选择CordovaLib,Scheme中选择CordovaLib,后面选择IOS Device,否则.a文件不生成,(使用模拟器的话,会读取电脑上的资源,所以需要选择ios device),然后编译

编译通过以后,选中上面的mall,点击Add Target,如下图:



添加完成以后,如下图,我们选则mall,点击上面的选项卡"Build Phases",在下面的Target Dependencies中点击左下方的加号,添加CordovaLib,如下图:

然后选在下面的”Link Binary with Libraries“,添加libCordova.a,如下图:


完成以后如下图:

选中刚刚添加成功的类库MallAgent,右键新建文件MainViewController继承自CDVViewController,完成以后如下图:


分别编辑MallAgent和MallViewController,代码分别入下:
MallAgent:
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <Cordova/CDVViewController.h>
#import "MainViewController.h"

@interface MallAgent : NSObject

+(MallAgent *)defaultInstance;

-(void)ShowView:(UIViewController *)parentviewcontroller;

@end

#import "MallAgent.h"

static MallAgent *instance = nil;

@interface MallAgent()

@property(nonatomic,strong)CDVViewController *viewcontroller;

@end

@implementation MallAgent

+(MallAgent *)defaultInstance{
    if(instance==nil){
        @synchronized(self){
            if(instance==nil){
                instance =[[MallAgent alloc] init];
            }
        }
    }
    return instance;
}

-(void)ShowView:(UIViewController *)parentviewcontroller{
    if(self.viewcontroller ==nil){
        self.viewcontroller =[[MainViewController alloc] initWithNibName:nil bundle:nil];
    }
    [parentviewcontroller presentViewController:self.viewcontroller animated:YES completion:nil];
}

@end

MainViewController:

#import <Cordova/CDVViewController.h>

@interface MainViewController : CDVViewController

@end

#import "MainViewController.h"

@interface MainViewController ()

@end

@implementation MainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

然后选择CordovaExamle,在"build setting"->"Search Paths"下,找到"User Header Search Paths",在后面的Value中输入值:
执行前面创建的MallAgent文件夹,在本例中我们输入:$(SRCROOT)/shopmall/platforms/ios/MallAgent,其中$(SRCROOT)是系统变量
然后切换到"build Phases",在Target Dependencies下面选在MallAgent,在"Link Binary with Libraries"中添加libMallAgent.a,同时添加如下图中的framework,


继续选在下面的"Copy Bundle Resource"添加下面的config.xml以及www目录,添加过程如下图:



www目录的选项



完成以后如下图:


编辑CordovaExample下面的LTViewController.h,在上面LTViewController.xib中添加一个Button,然后添加Click时间,代码如下:

#import <UIKit/UIKit.h>
#import "MallAgent.h"

@interface LTViewController : UIViewController

-(IBAction)BtnShow:(id)sender;

@end

#import "LTViewController.h"

@interface LTViewController ()

@end

@implementation LTViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

-(void)BtnShow:(id)sender{
    MallAgent *agent =[MallAgent defaultInstance];
    [agent ShowView:self];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

这样就可以运行了,运行以后界面如下:



说明:当然如果我们想要通过webView访问设置的话,我们可以通过cordova创建一个项目,直接在cordova中添加View,而不需要使用嵌套一个app的方式通过webView来访问设备

代码下载地址:http://download.csdn.net/detail/u011872945/7897347

抱歉!评论已关闭.