gpt4 book ai didi

javascript - 将变量从 objective-c 返回到 javascript

转载 作者:搜寻专家 更新时间:2023-10-30 20:01:09 24 4
gpt4 key购买 nike

我有一个 phonegap 应用程序,我想在 Documents 文件夹中运行一个非常简单的“文件是否存在”命令。我已经让它大部分工作了。在 js 中,我有:

fileDownloadMgr.fileexists("logo.png");
......
PixFileDownload.prototype.fileexists = function(filename) {
PhoneGap.exec("PixFileDownload.fileExists", filename);
};

然后在 Objective 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 中):

var bool = fileDownloadMgr.fileexists("logo.png");
if(bool) alert('The file is there!!!!!!');

我需要做什么才能将该 bool 从 objective c 返回到 javascript?

最佳答案

自从调用 PhoneGap.exec是异步的,你需要传递一个函数,当调用的 Objective-C 方法成功时调用该函数。使成功处理程序成为 fileexists 的参数(稍后解释原因):

PixFileDownload.prototype.fileexists = function(filename, success) {   
PhoneGap.exec(success, null, "PixFileDownload", "fileExists", filename);
};

PhoneGap.exec 的第二个参数是错误处理程序,此处未使用。

在 Obj-C 方法中,使用 PluginResult 通过 -resultWithStatus:messageAsInt: 方法将结果传递给成功函数。

-(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 方法可能导致错误情况,请使用 PluginResulttoErrorCallbackString: 创建调用错误函数的脚本。确保您还将错误处理程序作为第二个参数传递给 PhoneGap.exec

协调与延续

现在,将 success 参数添加到 fileexists 的 promise 解释。 “协调”是计算的一个特征,这意味着代码在它所依赖的任何计算完成之前不会运行。同步调用为您提供免费协调,因为函数在计算完成之前不会返回。对于异步调用,您需要注意协调。为此,您可以将依赖代码捆绑在一个名为“continuation”的函数中(这意味着“从给定点开始的其余计算”)并将此延续传递给异步函数。这被称为(毫不奇怪)continuation passing style (CPS)。请注意,您可以将 CPS 用于同步调用,只是不太常见。

PhoneGap.exec 是异步的,因此它接受延续,一个在成功时调用,一个在失败时调用。 fileexists 依赖于一个异步函数,因此它本身是异步的,需要传递一个延续。 fileDownloadMgr.fileexists("logo.png"); 之后的代码应该包含在传递给 fileexists 的函数中。例如,如果您最初有:

if (fileDownloadMgr.fileexists("logo.png")) {
...
} else {
...
}

创建一个延续很简单,但当你有多个延续时它可能会有点麻烦。将 if 语句重写为函数,用变量替换对异步函数的调用:

function (x) {
if (x) {
...
} else {
...
}
}

然后将此延续传递给 fileexists:

fileDownloadMgr.fileexists("logo.png", function (exists) {
if (exists) {
...
} else {
...
}
});

进一步阅读

我找不到 PluginResult-resultWithStatus:messageAsInt:,但是有一个例子展示了如何从 Obj-C 方法返回一个值到“How to Create a PhoneGap Plugin for iOS”中的 JS。 PhoneGap.exec 的文档API文档中的内容目前相当糟糕。由于两者都是 Wiki 页面,也许我或其他人会抽出时间来改进它们。还有 headerimplementation PluginResult 的文件。

关于javascript - 将变量从 objective-c 返回到 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7210287/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com