简体   繁体   English

如何在iPhone SDK中列出所有文件夹及其子目录/文件?

[英]How to list all folders and their subdirectories/files in iPhone SDK?

I want the user to select any file present in her/his iPhone so that it's used as an e-mail attachment. 我希望用户选择她/他的iPhone中存在的任何文件,以便将其用作电子邮件附件。 For this purpose, I want to show the list of all files and folders present in iPhone. 为此,我想显示iPhone中存在的所有文件和文件夹的列表。 But how would I get the list of those files? 但是我如何获得这些文件的列表? Is there any way to get that list? 有没有办法获得该名单?

Take into account that your app runs in a sandbox and would not be able to get any folder/file outside of that sandbox. 考虑到您的应用程序在沙箱中运行,并且无法获取该沙箱之外的任何文件夹/文件。

ObjectiveC 的ObjectiveC

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

NSFileManager *manager = [NSFileManager defaultManager];
NSArray *fileList = [manager contentsOfDirectoryAtPath:documentsDirectory error:nil];
for (NSString *s in fileList){
    NSLog(@"%@", s);
}

Swift 4 斯威夫特4

guard let documentsDirectory =  try? FileManager().url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true) else { return }
guard let fileEnumerator = FileManager.default.enumerator(at: documentsDirectory, includingPropertiesForKeys: nil, options: FileManager.DirectoryEnumerationOptions()) else { return }
while let file = fileEnumerator.nextObject() {
    print(file)
}

Here's the slowest(?) approach: 这是最慢的(?)方法:

NSFileManager * fileManager = [NSFileManager new];
NSArray * subpaths = [fileManager subpathsAtPath:path];

but that should at least point you to a more specialized implementation for your needs. 但这至少应该指向一个更专业的实现来满足您的需求。

Slightly lower level abstractions which allow you to enumerate on demand include NSDirectoryEnumerator and CFURLEnumerator . 允许您按需枚举的略低级别的抽象包括NSDirectoryEnumeratorCFURLEnumerator Depending on the depth of the directory, these have the potential to save much unnecessary interactions with the filesystem, compared to -[NSFileManager subpathsAtPath:] . -[NSFileManager subpathsAtPath:]相比,根据目录的深度,它们有可能与文件系统保存很多不必要的交互。

You can use NSDirectoryEnumerator via NSFileManager.enumeratorAtPath 您可以使用NSDirectoryEnumerator通过NSFileManager.enumeratorAtPath

From the docs: 来自文档:

NSString *docsDir = [NSHomeDirectory() stringByAppendingPathComponent:  @"Documents"];
NSFileManager *localFileManager=[[NSFileManager alloc] init];
NSDirectoryEnumerator *dirEnum =
    [localFileManager enumeratorAtPath:docsDir];

NSString *file;
while ((file = [dirEnum nextObject])) {
    if ([[file pathExtension] isEqualToString: @"doc"]) {
        // process the document
        [self scanDocument: [docsDir stringByAppendingPathComponent:file]];
    }
}

swift 3 迅捷3

    let fileManager:FileManager = FileManager()
    let files = fileManager.enumerator(atPath: NSHomeDirectory())
    while let file = files?.nextObject() {
        print("Files::",file)
    }

I'm an author of FileExplorer control which is a file browser for iOS and fulfills most of your requirements. 我是FileExplorer控件的作者,它是iOS的文件浏览器,可满足您的大部分要求。 Note that it allows you to browse only those files and directories that are placed inside your sandbox. 请注意,它允许您仅浏览放置在沙箱中的文件和目录。

Here are some of the features of my control: 以下是我控制的一些功能:

  1. Possibility to choose files or/and directories if there is a need for that 如果需要,可以选择文件或/和目录
  2. Possiblity to remove files or/and directories if there is a need for that 如果需要,可以删除文件或/和目录
  3. Built-in search functionality 内置搜索功能
  4. View Audio, Video, Image and PDF files. 查看音频,视频,图像和PDF文件。
  5. Possibility to add support for any file type. 可以添加对任何文件类型的支持。

You can find my control here . 你可以在这里找到我的控件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM