1

我正在使用 UIDocumentPickerViewController 来允许用户选择将“附加”并在应用程序中可用的文件。这个概念是允许用户通过电子邮件发送带有所选文件附件的详细信息。
附加每个文件后,我将文件从 tmp 收件箱(fileManager 将导入的文件放在其中)复制到我在 App 文档目录中创建的名为“fileAttachments”的目录中。
我在 UITableView 中列出文件,用户可以选择每个条目并使用存储在文件对象 fileOJ.filePath 中的路径预览 QLPreviewController 视图中的内容。
这一切都运行良好,直到将项目重新加载到我的测试 iPad 上,然后所有文件似乎都消失了。我的文件列表仍然很好,但路径位置没有文件。
任何有关正在发生的事情的帮助将不胜感激。

    - (IBAction)selectFilesAction:(UIBarButtonItem *)sender {

        NSArray *UTIs = [NSArray arrayWithObjects:@"public.data", nil];
        [self openFilePicker:UTIs];

    }

    - (void)openFilePicker:(NSArray *)UTIs {
            UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:UTIs inMode:UIDocumentPickerModeImport];
            documentPicker.delegate = self;
            documentPicker.allowsMultipleSelection = FALSE;
            documentPicker.popoverPresentationController.barButtonItem = self.selectFilesButton;
            [self presentViewController:documentPicker animated:TRUE completion:nil];
    }

    - (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray <NSURL *>*)urls {
        NSLog(@"picked URLs %@", urls);
        // selecting multiple documents is cool, but requires iOS 11

        for (NSURL *documentURL in urls) {
            //get file details
            NSDictionary *attr = [documentURL resourceValuesForKeys:@[NSURLFileSizeKey,NSURLCreationDateKey] error:nil];
            NSLog(@"object: %@", attr);

            NSNumber *fileSize = [attr valueForKey:NSURLFileSizeKey];
            NSDate *dateFileCreated = [attr valueForKey:NSURLCreationDateKey];

            NSDateFormatter *storageDateFormat = [[NSDateFormatter alloc] init];
            [storageDateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
            NSString *createdDateString = [storageDateFormat stringFromDate:dateFileCreated];

            MMfile *fileObj = [[MMfile alloc]init];
            fileObj.fileName = documentURL.lastPathComponent;
            fileObj.meetingID = _meetingID;
            fileObj.fileSize = fileSize;
            fileObj.fileCreateDate = createdDateString;

            //move file to new directory
            fileObj.filePath = [self movefile:documentURL.lastPathComponent sourceFilePath:documentURL.path directory:@"fileAttachments"];

            //save file details
            [self.meetingModel saveFile:fileObj];

            //refresh array and reload table
            self.fileArray = [self.meetingModel getFiles:self.meetingID];
            [self.tableView reloadData];
        }
    }


    - (void)documentPickerWasCancelled:(UIDocumentPickerViewController *)controller {
        NSLog(@"cancelled");
    }

    -(NSString *)movefile:(NSString *)filename sourceFilePath:(NSString *)sourcePath directory:(NSString *)directoryName{
        // Move file from tmp Inbox to the destination directory
        BOOL isDir;
        NSError *error;
        NSFileManager *fileManager= [NSFileManager defaultManager];

        //get directory path
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
        NSString* directoryPath;

        if (paths>0) {
            NSString *documentsDirectory = [paths objectAtIndex:0];
            directoryPath = [NSString stringWithFormat:@"%@/%@",documentsDirectory,directoryName];
        }

        if(![fileManager fileExistsAtPath:directoryPath isDirectory:&isDir])
            if(![fileManager createDirectoryAtPath:directoryPath withIntermediateDirectories:NO attributes:nil error:NULL])
                NSLog(@"Error: Create folder failed %@", directoryPath);

        NSString *destinationPath = [NSString stringWithFormat:@"%@/%@",directoryPath,filename];;
        BOOL success = [fileManager moveItemAtPath:sourcePath toPath:destinationPath error:&error];
        if (success) {
            NSLog(@"moved file");

        }else{
            NSLog(@"error %@",error.description);
        }
        return destinationPath;
    }
4

1 回答 1

1

发现了问题。当项目重新构建并下载到 iPad 时,AppID 会发生变化,并且由于文档路径包含 AppID,因此文档路径也会发生变化。关键不是保存文件路径,只有文件名和重建每个实例的路径。找到问题后,我现在看到了我之前没有找到的其他类似帖子。另请参阅重建应用程序时的文档目录路径更改

于 2020-05-02T22:37:00.250 回答