UIDocumentPickerViewController复制的文件在下载到测试iPad时消失。

问题描述 投票:0回答:1

我正在使用UIDocumentPickerViewController来允许用户选择将被 "附加 "的文件并在App中使用。其概念是允许用户通过电子邮件发送带有所选文件附件的详细信息。当每个文件被附加时,我将文件从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;
    }
ipad uidocumentpickerviewcontroller
1个回答
0
投票

找到了问题所在。当项目重建并下载到iPad时,AppID会发生变化,由于文档路径包含AppID,所以文档路径也会发生变化。关键是不保存文件路径,只保存文件名,每次都要重建路径。 找到问题后,现在看到其他类似的帖子,我之前没有找到。 还可以看到 重建应用程序时,文档目录路径发生变化

© www.soinside.com 2019 - 2024. All rights reserved.