我正在为 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/