phonegap 创建项目命令 以及使用

使用phonegae框架,可以通过html和js脚本实现ios的开发。下面介绍框架的使用,觉得讲的好,评论赞个。。O(∩_∩)O哈哈~

原创内容,转载请注明~~~


1、        创建项目

找到phonegae包的的create ~/phonegap-2.9.1/lib/ios/bin/create

执行 ~/phonegap-2.9.1/lib/ios/bin/create moatest[项目文件夹名] moatest[Bundle identifier] moatest[项目名称]

 

 

2、        设置启动的页面

在程序委托中,设置如下代码:

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions

{

    CGRect screenBounds = [[UIScreen mainScreen] bounds];

 

#if __has_feature(objc_arc)

        self.window = [[UIWindow alloc] initWithFrame:screenBounds];

#else

        self.window = [[[UIWindow alloc] initWithFrame:screenBounds] autorelease];

#endif

    self.window.autoresizesSubviews = YES;

 

#if __has_feature(objc_arc)

        self.viewController = [[MainViewController alloc] init];

#else

        self.viewController = [[[MainViewController alloc] init] autorelease];

#endif

    self.viewController.useSplashScreen = YES;

    //使用url加载

    self.viewController.wwwFolderName = @"www";

    self.viewController.startPage = @"index.html";

   

    self.window.rootViewController = self.viewController;

    [self.window makeKeyAndVisible];

 

    return YES;

}

通过设定startPage加载本地页面,亦可以加载外部链接

 

3、        在phonegae中使用插件的方式,实现js脚本与oc做交互

在页面中又如下脚本:

<button width='320'height='200'onclick="callWebviewMethoth()">插件回调</button>

 

js脚本:

//回调方法

            var success=function(msg){

                alert("成功:"+msg);

           }

       

           var fail=function(msg){

                alert("失败:"+msg);

            }

     functioncallWebviewMethoth()

     {

                console.log("callBackFunction called.....");

                var parm=["参数1","参数2"];

                Cordova.exec(success, fail, "PluginService", "openWebView",parm);

 }

     其中,Cordova.exec()是调用的核心代码,success,fail为 成功失败的回调方法,PluginService为config.xml配置的插件名称,openWebView为对应oc类中的方法名,parm为将要传入的参数

接着在项目的config.xml中配置如下,指定插件:

<feature name="PluginService">

        <paramname="ios-package"value="PluginService"/>

</feature>

其中,<featurename="PluginService">指定了插件名称,<param name="ios-package"value="PluginService"/>指定了对应的oc类。

 

下面,创建PluginServiceoc类,

#import <Cordova/CDVPlugin.h>

@interface PluginService : CDVPlugin<UIAlertViewDelegate,BFWebViewDelegate>{

    CDVPluginResult* _pluginResult;

    CDVInvokedUrlCommand * _command;

    BFWebView *bfWeb;

}

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

@end

 

 

方法实现:

- (void)openWebView:(CDVInvokedUrlCommand*)command{

    BRELEASE(_pluginResult);

    BRELEASE(_command);

//    _pluginResult = nil;

    _command=[command retain];

    NSString* myarg = [command.arguments objectAtIndex:0];

   

    AppDelegate *app=[AppDelegate shareInstance];

    

    if (myarg != nil) {

        CGFloat vHeight=0;

        if (IOS7) {

            vHeight=20;

        }

        bfWeb=[[BFWebView alloc] initWithFrame:CGRectMake(0,vHeight, [self getViewWidth], [self getViewHeight])];

        bfWeb.delegate=self;

        [bfWeb openUrl:myarg];

        [app.viewController.view addSubview:bfWeb];

 

        _pluginResult = [[CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:myarg] retain];

    } else {

        _pluginResult = [[CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Argwas null"] retain];

    }

   [self.commandDelegate evalJs:@"-----thisdelegetgive-----"];

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

}

 

其中,_pluginResult = [[CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:myarg] retain];为返回给js的值,成功通过CDVCommandStatus_OK传,失败通过resultWithStatus:CDVCommandStatus_ERROR传,js将会调用之前设置的回调方法。


你可能感兴趣的:(js,ios,APP,插件,PhoneGap)