ios5 - 如何将回调值传递给 JavaScript 形式的 iOS 原生代码?

标签 ios5 ios4 cordova phonegap-plugins

我正在为 iOS 创建 phoneGap 插件,它使用 NativeCode 绘制签名并将回调值传递给 JavaScript。

我能够使用 natice 代码成功绘制签名,但我无法将已保存签名的返回值传递给 JavaScript。

JavaScript代码如下

var MyPlugin = {
 nativeFunction: function(types, success, fail) {
      return Cordova.exec(success, fail, "MyPlugin", "print", types);
 }
};

在“打印”函数中,我调用了一个绘制签名的 UIViewContoller 类,在“保存”按钮上,我想将返回值传递给 JavaScript。

点击保存按钮的代码

// The first argument in the arguments parameter is the callbackID.
// We use this to send data back to the successCallback or failureCallback
// through PluginResult
self.callbackID = [arguments pop];

// Get the string that javascript sent us
NSString *stringObtainedFromJavascript = [arguments objectAtIndex:0];                 

// Create the Message that we wish to send to javascript   
NSMutableString *stringToReturn = [NSMutableString stringWithString: @"StringReceived:"];

// Append the received string to the string we plan to send out        
[stringToReturn appendString: stringObtainedFromJavascript];

// Create Plugin Result 
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
                                                  messageAsString: [stringToReturn stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

// Checking if the string received is HelloWorld or not    
 if ([stringObtainedFromJavascript isEqualToString:@"SAVED"] == YES)    
 {   
 // Call the javascript success function
 [self writeJavascript: [pluginResult toSuccessCallbackString:self.callbackID]];
 }else    
 {    
 // Call the javascript error function
 [self writeJavascript: [pluginResult toErrorCallbackString:self.callbackID]];
 }

我没有收到任何错误或任何异常。

谁能帮帮我。任何建议将不胜感激。

提前致谢。

最佳答案

看看这个 GitHub 项目:https://github.com/rohdef/PGPlugins

有一些为 phonegap 编写插件的好例子。

关于ios5 - 如何将回调值传递给 JavaScript 形式的 iOS 原生代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11309602/

相关文章:

audio - 使用iOS 5 Mobile Safari自动播放音频,还有其他解决方法吗?

iphone - 如何卸载 Storyboard?

android - 加载资源失败:net::ERR_CLEARTEXT_NOT_PERMITTED

ios - 在常规 UIViewController 上的 UITableViewCell 中的键盘上方滚动 UITextField

objective-c - 错误 '!dat'

ios - 本地通知phonegap ios

windows - Cordova 窗口错误 : "cordova/windows8/commandProxy" not found

iphone - 在 Storyboard 中创建 TabBarControllerDelegate

iphone - 使用新的外部存储标志将 UIImage 存储在 Core Data 中

iphone - 如何删除 UIWindow?