phonegap 2.9版本插件开发

phonegap升级到2.9版本后,配置方法与之前产生了一下变化。

在此描述一下2.9版本的IOS插件编写方法,仅供参考:


用xcode向导创建插件类MyPlugin,添加add方法。

add方法实现如下:

#import "MyPlugin.h"


@implementation MyPlugin

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

    CDVPluginResult* pluginResult = nil;

    NSString* javaScript = nil;

    

    @try {

        

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

        NSString* echo2 = [command.arguments objectAtIndex:1];

        

        if (echo != nil

            && [echo length] > 0

            && echo2 != nil

            && [echo2 length] > 0) {

            

            /**

             *相加字符串

             */

            NSString *addResult = [NSString stringWithFormat:@"%@%@", echo, echo2];

            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:addResult];

            javaScript = [pluginResult toSuccessCallbackString:command.callbackId];

            

            

        } else {

            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];

            javaScript = [pluginResult toErrorCallbackString:command.callbackId];

        }

    } @catch (NSException* exception) {

        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_JSON_EXCEPTION messageAsString:[exception reason]];

        javaScript = [pluginResult toErrorCallbackString:command.callbackId];

    }

    

    [self writeJavascript:javaScript];

}

@end


创建MyPlugin.js 如下图。





在config.xml中配置

<plugins>

       <plugin name="MyPlugin" value="MyPlugin" />

 </plugins>

注意: name 值要跟MyPlugin.js中

cordova.exec(addSuc, addFaild, "MyPlugin", "add", [args,args2]);函数的第3个参数一致。

value=""的值要与自定义插件的类名一直。


然后在你的html中引入MyPlugin.js ,就能够调用自定义的原生插件了。本例的调用方法参考如下


<script>

  function addSuc(result) {

                console.log('addSuc='+result);

                $("#af15_content").append(result);

            }

            function addFiald() {

                console.log('addFiald');

            }

            

            function Addstring(){

                

                alert("Addstring");

                TestPlugin.add("My", "-Plugin!",addSuc,addFiald);

                

            }

</script>
自定义插件方法的返回值要通过 addSuc回调函数的参数返回。
特别注意:

cordova.exec方法调用必须在页面加载完成后才能调用,否则会调用不成功。

页面也必须加载cordova.js.



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值