如何获取文件列表/上传/重命名/写入/读取文件在iOS设备编程?

我正在创build一个应用程序,用户将不得不上传文件和图像如xls,pdf,txt,jpg,png等。我想向用户展示他的iOS设备中存在的所有文件,请帮助我。

首先,您应该阅读Apple文档中的NSFileManager概念,然后自动地了解如何执行此操作:

你可以访问的只是在你的应用程序中,仅此而已 –

你能看到下面的代码。 我希望这会对你有所帮助

(1). #pragma mark #pragma mark -- list all the files exists in Document Folder in our Sandbox. - (void)listAllLocalFiles{ // Fetch directory path of document for local application. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; // NSFileManager is the manager organize all the files on device. NSFileManager *manager = [NSFileManager defaultManager]; // This function will return all of the files' Name as an array of NSString. NSArray *files = [manager contentsOfDirectoryAtPath:documentsDirectory error:nil]; // Log the Path of document directory. NSLog(@"Directory: %@", documentsDirectory); // For each file, log the name of it. for (NSString *file in files) { NSLog(@"File at: %@", file); } } (2). #pragma mark #pragma mark -- Create a File in the Document Folder. - (void)createFileWithName:(NSString *)fileName{ NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName]; NSFileManager *manager = [NSFileManager defaultManager]; // 1st, This funcion could allow you to create a file with initial contents. // 2nd, You could specify the attributes of values for the owner, group, and permissions. // Here we use nil, which means we use default values for these attibutes. // 3rd, it will return YES if NSFileManager create it successfully or it exists already. if ([manager createFileAtPath:filePath contents:nil attributes:nil]) { NSLog(@"Created the File Successfully."); } else { NSLog(@"Failed to Create the File"); } } (3). #pragma mark #pragma mark -- Delete a File in the Document Folder. - (void)deleteFileWithName:(NSString *)fileName{ NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; // Have the absolute path of file named fileName by joining the document path with fileName, separated by path separator. NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName]; NSFileManager *manager = [NSFileManager defaultManager]; // Need to check if the to be deleted file exists. if ([manager fileExistsAtPath:filePath]) { NSError *error = nil; // This function also returnsYES if the item was removed successfully or if path was nil. // Returns NO if an error occurred. [manager removeItemAtPath:filePath error:&error]; if (error) { NSLog(@"There is an Error: %@", error); } } else { NSLog(@"File %@ doesn't exists", fileName); } } (4). #pragma mark #pragma mark -- Rename a File in the Document Folder. - (void)renameFileWithName:(NSString *)srcName toName:(NSString *)dstName{ NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *filePathSrc = [documentsDirectory stringByAppendingPathComponent:srcName]; NSString *filePathDst = [documentsDirectory stringByAppendingPathComponent:dstName]; NSFileManager *manager = [NSFileManager defaultManager]; if ([manager fileExistsAtPath:filePathSrc]) { NSError *error = nil; [manager moveItemAtPath:filePathSrc toPath:filePathDst error:&error]; if (error) { NSLog(@"There is an Error: %@", error); } } else { NSLog(@"File %@ doesn't exists", srcName); } } (5).#pragma mark #pragma mark -- Read a File in the Document Folder. /* This function read content from the file named fileName. */ - (void)readFileWithName:(NSString *)fileName{ // Fetch directory path of document for local application. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; // Have the absolute path of file named fileName by joining the document path with fileName, separated by path separator. NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName]; // NSFileManager is the manager organize all the files on device. NSFileManager *manager = [NSFileManager defaultManager]; if ([manager fileExistsAtPath:filePath]) { // Start to Read. NSError *error = nil; NSString *content = [NSString stringWithContentsOfFile:filePath encoding:NSStringEncodingConversionAllowLossy error:&error]; NSLog(@"File Content: %@", content); if (error) { NSLog(@"There is an Error: %@", error); } } else { NSLog(@"File %@ doesn't exists", fileName); } } (6). #pragma mark #pragma mark -- Write a File in the Document Folder. /* This function Write "content" to the file named fileName. */ - (void)writeString:(NSString *)content toFile:(NSString *)fileName{ // Fetch directory path of document for local application. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; // Have the absolute path of file named fileName by joining the document path with fileName, separated by path separator. NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName]; // NSFileManager is the manager organize all the files on device. NSFileManager *manager = [NSFileManager defaultManager]; // Check if the file named fileName exists. if ([manager fileExistsAtPath:filePath]) { NSError *error = nil; // Since [writeToFile: atomically: encoding: error:] will overwrite all the existing contents in the file, you could keep the content temperatorily, then append content to it, and assign it back to content. // To use it, simply uncomment it. // NSString *tmp = [[NSString alloc] initWithContentsOfFile:fileName usedEncoding:NSStringEncodingConversionAllowLossy error:nil]; // if (tmp) { // content = [tmp stringByAppendingString:content]; // } // Write NSString content to the file. [content writeToFile:filePath atomically:YES encoding:NSStringEncodingConversionAllowLossy error:&error]; // If error happens, log it. if (error) { NSLog(@"There is an Error: %@", error); } } else { // If the file doesn't exists, log it. NSLog(@"File %@ doesn't exists", fileName); } // This function could also be written without NSFileManager checking on the existence of file, // since the system will atomatically create it for you if it doesn't exist. } 

你想在iOS中不可能的。 您创build的应用程序只能访问“文档”文件夹中的文件。

没有“电话中的所有文件”的概念,每个应用程序pipe理它自己的文件。 您可以与其他应用程序交互的唯一方法是通过应用程序开发人员提供的公共API。

如果你想获得你的Documents目录中的所有文件,你可以这样获得path:

 NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsPath = [searchPaths objectAtIndex:0]; 

您还可以访问用户的照片库,您可以使用ALAssets (最多iOS7)或PHAssets (iOS 8或更高 )进行交互。

希望这可以帮助。