其中一位开发人员正在实施支持跨平台(ios& android)的phonegap项目。只要有任何可用于ios的插件,事情就会变得奇怪。我已经筋疲力尽了已经出现的要求,我们将应用程序中生成的pdf文件保存到文档目录中。在android中,当单击选择文件时,会打开文件浏览器以从所需位置选择文件。我必须为ios编写一个自定义插件,因为事情是在ios中沙箱化的...用户可以选择的唯一方式是保存在app文档目录中的文件。我试图从目录中获取文档(pdf文件)并在表视图中填充它们。实际上我在Git存储库上遵循了这个link来保存pdf文件。该链接遵循传统的文档目录保存方式,但我看不到文件是通过扩展名保存的。#pdf'令人惊讶的是pdf文件可见,其中temp目录中的扩展名嵌入了唯一文件夹。此外,在用户在适当的阅读器s / w中打开文件后,它会从文档目录中删除文件。
当我尝试使用以下代码从临时目录中检索文件时:
NSString *docPath = NSTemporaryDirectory();
self.filesArray = [[[NSFileManager defaultManager] contentsOfDirectoryAtPath:docPath error:NULL] mutableCopy];
for (NSString *file in _filesArray) {
NSString *actualFile = [NSString stringWithFormat:@"/%@",file];
docPath = [docPath stringByAppendingString:actualFile];
}
[_filePathsArray addObject:docPath];
正如所料,它只返回目录(我的意思是文件夹)....而pdf文件隐藏在这些子目录中。我需要提取这些文件并显示在表视图中。以下是我创建的插件的示例代码:
- (NSMutableDictionary *)callbackIds {
if (_callbackIds == nil) {
_callbackIds = [[NSMutableDictionary alloc] init];
}
return _callbackIds;
}
-(void)open:(CDVInvokedUrlCommand *)command
{
[self.callbackIds setValue:command.callbackId forKey:@"open"];
CGFloat width = [UIScreen mainScreen].bounds.size.width;
CGFloat height = [UIScreen mainScreen].bounds.size.height;
self.listOfFiles = [[UITableView alloc] initWithFrame:CGRectMake(230, 210, width/1.8, height/1.8) style:UITableViewStylePlain];
self.listOfFiles.dataSource = self;
self.listOfFiles.delegate = self;
self.listOfFiles.scrollEnabled = YES;
self.listOfFiles.rowHeight = tableHeight;
[self.viewController.view addSubview:_listOfFiles];
}
#pragma mark - TableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
self.filePathsArray = [[NSMutableArray alloc]init];
self.filesArray = [[NSMutableArray alloc]init];
NSString *docPath = NSTemporaryDirectory();
self.filesArray = [[[NSFileManager defaultManager] contentsOfDirectoryAtPath:docPath error:NULL] mutableCopy];
for (NSString *file in _filesArray) {
NSString *actualFile = [NSString stringWithFormat:@"/%@",file];
docPath = [docPath stringByAppendingString:actualFile];
}
[_filePathsArray addObject:docPath];
return _filesArray.count;
}
#pragma mark - TableViewDelegate
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [_filesArray objectAtIndex:indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
// Send result back to the javascript plugin
NSString *resultingPath = [self.filePathsArray objectAtIndex:indexPath.row];
[self returnPluginResult:resultingPath toCallback:@"open" withError:NO];
[_listOfFiles removeFromSuperview];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return @"Choose File";
}
#pragma mark - Plugin Callback
- (void)returnPluginResult:(NSString *)result toCallback:(NSString *)callback withError:(BOOL)error
{
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:result];
if (!error) {
[self writeJavascript:[pluginResult toSuccessCallbackString:[self.callbackIds valueForKey:callback]]];
}
else {
[self writeJavascript:[pluginResult toErrorCallbackString:[self.callbackIds valueForKey:callback]]];
}
}
有人请指导我解决方案或随后的有用链接。我尝试了大量的代码而不是大量的谷歌搜索,似乎没有什么能解决我的问题。
提前感谢大家:)