我有一个phonegap应用程序,我想在Documents文件夹中运行一个非常简单的“存在文件”命令.我得到它主要工作.在js中,我有:
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中):
var bool = fileDownloadMgr.fileexists("logo.png");
if(bool) alert('The file is there!!!!!!');
我需要做什么才能将目标c中的bool返回到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:方法将结果传递给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的第二个参数传递.
协调与协调延续
现在,将成功参数添加到fileexists的承诺解释. “协调”是计算的一个特征,意味着代码在其依赖的任何计算完成之前不会运行.同步调用为您提供免费协调,因为函数在计算完成之前不会返回.使用异步调用,您需要注意协调.你可以通过将依赖代码捆绑在一个名为“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:,但是有一个示例显示如何在“How to Create a PhoneGap Plugin for iOS”中将Obj-C方法的值返回给JS. API文档中的PhoneGap.exec
文档目前相当差.既然两者都是维基页面,也许我或其他人会找到时间来改进它们.还有PluginResult的header和implementation文件.
原文地址:https://codeday.me/bug/20190826/1733921.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。