【问题标题】:Phonegap jsCallback in Objective CObjective C中的Phonegap jsCallback
【发布时间】:2014-03-11 03:04:22
【问题描述】:

所以我知道我们可以通过以下方式调用本机函数 cordova.exec(成功,失败,...

而不是像关联回调

ChildBrowser._onClose = function()
{
    window.plugins.childBrowser.onClose();
};

而不是做类似的事情:

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

当我成功完成某个场景时,如何调用我的success回调函数?

【问题讨论】:

    标签: objective-c cordova


    【解决方案1】:

    尝试将此示例应用到您的项目中。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-28
      • 2012-07-17
      相关资源
      最近更新 更多