【问题标题】:Unable to store/retrieve files properly from NSDocumentsDirectory无法从 NSDocumentsDirectory 正确存储/检索文件
【发布时间】:2025-12-05 10:15:02
【问题描述】:

其中一位开发人员正在实施一个支持跨平台(ios 和 android)的 phonegap 项目。每当没有任何适用于 ios 的插件时,事情就会变得很奇怪。我对提出的要求感到筋疲力尽,我们将应用程序中生成的 pdf 文件保存到文档目录中。在 android 中,当单击选择文件时,会打开一个文件资源管理器以从所需位置选择文件。我不得不为 ios 编写一个自定义插件,因为东西在 ios 中被沙盒化了……用户可以选择的唯一方法是保存在应用程序文档目录中的文件。我正在尝试从目录中获取文档(pdf 文件)并将它们填充到表格视图中。实际上,我在 Git 存储库上关注了这个 link 来保存 pdf 文件。该链接遵循保存在文档目录中的传统方式,但我没有看到使用扩展名“.pdf”保存的文件,令人惊讶的是,pdf 文件在临时目录中嵌入了唯一文件夹中的扩展名是可见的。此外,当用户在适当的阅读器软件中打开文件后,它会从文档目录中清除文件。

当我尝试使用以下代码从临时目录中检索文件时:

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]]];
    }
}

请有人指导我提供解决方案或后续有用的链接。我通过大量的谷歌搜索尝试了所有代码示例,但似乎没有任何问题可以解决我的问题。

提前谢谢大家:)

【问题讨论】:

    标签: ipad pdf-generation phonegap-plugins filechooser


    【解决方案1】:

    我遵循的用于存储 pdf 的 link 实际上是在存储时从文档目录中删除文件。截断删除代码为我解决了这个问题。希望这对某人有所帮助,谢谢:)

    【讨论】: