在调用时出现延迟对象和phonegap插件问题

时间:2014-03-18 14:38:26

标签: jquery ios cordova phonegap-plugins deferred

如何执行以下

[self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:**response**] callbackId:command.callbackId ];

然后将结果作为

$.when(phonegap.function(params)).then(function (**resp**) {
//Get the response here 

});

1 个答案:

答案 0 :(得分:0)

您需要使用cordova.exec API才能使其正常工作

确保您已在config.xml中定义插件

<feature name="CustomPlugin">
      <param name="ios-package" value="CustomPlugin" />
</feature>

使用Objective-C代码实现插件

CustomPlugin.h

#import <Foundation/Foundation.h>
#import <Cordova/CDV.h>

@interface CustomPlugin : CDVPlugin

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

@end

CustomPlugin.m

#import "CustomPlugin.h"

    @implementation CustomPlugin

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

        NSString *responseString =
            [NSString stringWithFormat:@"Hello World, %@", [command.arguments objectAtIndex:0]];

        CDVPluginResult *pluginResult =
            [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:responseString];

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

    @end

从JavaScript调用插件

function initial(){
    var name = $("#NameInput").val();
    cordova.exec(sayHelloSuccess, sayHelloFailure, "CustomPlugin", "sayHello", [name]);
}

function sayHelloSuccess(data){
    alert("OK: " + data);
}

function sayHelloFailure(data){
    alert("FAIL: " + data);
}