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

后端 未结 5 1400
野趣味
野趣味 2020-12-14 12:00

I want the user to select any file present in her/his iPhone so that it’s used as an e-mail attachment. For this purpose, I want to show the list of all files and folders pr

5条回答
  •  南笙
    南笙 (楼主)
    2020-12-14 13:03

    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

    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

    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)
    }
    

提交回复
热议问题