将变量从objective c返回到javascript
我有一个phonegap应用程序,我想在Documents文件夹中运行一个非常简单的“doesfile exist”命令。我已经让它大部分工作。在js中,我有:将变量从objective c返回到javascript,javascript,objective-c,cordova,Javascript,Objective C,Cordova,我有一个phonegap应用程序,我想在Documents文件夹中运行一个非常简单的“doesfile exist”命令。我已经让它大部分工作。在js中,我有: fileDownloadMgr.fileexists("logo.png"); ...... PixFileDownload.prototype.fileexists = function(filename) { PhoneGap.exec("PixFileDownload.fileExists", filename);
fileDownloadMgr.fileexists("logo.png");
......
PixFileDownload.prototype.fileexists = function(filename) {
PhoneGap.exec("PixFileDownload.fileExists", filename);
};
在目标C中,我有:
-(BOOL) fileExists:(NSMutableArray*)paramArray withDict:(NSMutableDictionary*)options;{
NSString * fileName = [paramArray objectAtIndex:0];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *newFilePath = [documentsDirectory stringByAppendingString:[NSString stringWithFormat: @"/%@", fileName]];
BOOL isMyFileThere = [[NSFileManager defaultManager] fileExistsAtPath:newFilePath];
//i'm stuck here
}
我可以使用NSLog将其打印到控制台,以查看逻辑是否正常工作以及BOOL设置是否正确。但我需要这个变量回到javascript世界。我知道stringByEvaluatingJavaScriptFromString,但这只会执行javascript,即调用回调函数。这不是我在这里需要的,我需要(在javascript中):
我需要做什么才能将bool从objective c返回到javascript?由于对的调用是异步的,因此需要传递一个函数,当调用的objective-c方法成功时调用该函数。将成功处理程序设为fileexists
的参数(稍后解释原因):
PhoneGap.exec
的第二个参数是错误处理程序,此处未使用
在Obj-C方法中,使用PluginResult
通过-resultWithStatus:messageAsInt:
方法将结果传递给success函数
-(BOOL) fileExists:(NSMutableArray*)paramArray withDict:(NSMutableDictionary*)options;{
...
//i'm stuck here
/* Create the result */
PluginResult* pluginResult = [PluginResult resultWithStatus:PGCommandStatus_OK
messageAsInt:isMyFileThere];
/* Create JS to call the success function with the result */
NSString *successScript = [pluginResult toSuccessCallbackString:self.callbackID];
/* Output the script */
[self writeJavascript:successScript];
/* The last two lines can be combined; they were separated to illustrate each
* step.
*/
//[self writeJavascript: [pluginResult toSuccessCallbackString:self.callbackID]];
}
如果Obj-C方法可能导致错误情况,请使用PluginResult
的toErrorCallbackString:
创建调用错误函数的脚本。确保还将错误处理程序作为第二个参数传递给PhoneGap.exec
协调与延续
现在,向文件添加success
参数的承诺解释已经存在。“协调”是计算的一个特性,这意味着代码在它所依赖的任何计算完成之前不会运行。同步调用为您提供了免费的协调,因为函数在计算完成之前不会返回。对于异步调用,您需要注意协调。通过将依赖代码绑定到一个名为“”的函数(意思是“从给定点开始的剩余计算”)中,并将该延续传递给异步函数,可以实现这一点。这被称为(毫不奇怪)(CPS)。请注意,您可以将CPS用于同步调用,但这并不常见
PhoneGap.exec
是异步的,因此它接受continuations,一个在成功时调用,一个在失败时调用fileexists
依赖于异步函数,因此它本身是异步的,需要传递一个延续。fileDownloadMgr.fileexists(“logo.png”)之后的代码代码>应包装在传递给fileexists
的函数中。例如,如果您最初有:
if (fileDownloadMgr.fileexists("logo.png")) {
...
} else {
...
}
创建一个延续是很简单的,但是当您有多个延续时,它可能会有点毛茸茸的。将if
语句重写为函数,用变量替换对异步函数的调用:
function (x) {
if (x) {
...
} else {
...
}
}
然后将此延续传递到文件exists
:
fileDownloadMgr.fileexists("logo.png", function (exists) {
if (exists) {
...
} else {
...
}
});
进一步阅读
我找不到PluginResult的-resultWithStatus:messageAsInt:
,但有一个示例演示了如何将值从Obj-C方法返回到“”中的JS。API文档中的文档目前相当糟糕。由于两者都是维基页面,也许我或其他人会抽出时间来改进它们。还有用于PluginResult
outis的和文件,我将向您介绍继续部分的实现。你能提供一个更清楚的例子吗?谢谢
function (x) {
if (x) {
...
} else {
...
}
}
fileDownloadMgr.fileexists("logo.png", function (exists) {
if (exists) {
...
} else {
...
}
});