1

UIDocumentPickerViewController を使用して、ユーザーが「添付」されてアプリ内で利用可能なファイルを選択できるようにしています。コンセプトは、ユーザーが選択したファイルを添付して電子メールで詳細を送信できるようにすることです。
各ファイルが添付されると、ファイルを tmp Inbox (fileManager がインポートされたファイルを配置する場所) から、「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