目标C中的Phonegap jsCallback

时间:2014-03-11 03:04:23

标签: objective-c cordova

所以我明白我们可以通过调用本机函数 cordova.exec(的成功下,故障,...

而不是像

那样关联回调
ChildBrowser._onClose = function()
{
    window.plugins.childBrowser.onClose();
};

而不是做类似的事情:

NSString* jsCallback = [NSString stringWithFormat:@"window.plugins.childBrowser.onClose('%@');", (NSString*) booleanString];

成功完成某些方案后,如何调用成功回调函数?

1 个答案:

答案 0 :(得分:0)

尝试在您的项目中实施此示例。

确保您已在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);
}