Muhammad Saqib Muhammad Saqib - 1 year ago 69
iOS Question

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. For this purpose, I want to show the list of all files and folders present in iPhone. But how would I get the list of those files? Is there any way to get that list?

Answer Source

This code assumes that you have access to the files and folders. Take into account that your app runs in a sandbox and would not be able to get any folder/file outside of that sandbox.


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


let documentsDirectory =  try! NSFileManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true)
let fileEnumerator = NSFileManager.defaultManager().enumeratorAtURL(documentsDirectory, includingPropertiesForKeys: nil, options: NSDirectoryEnumerationOptions(), errorHandler: nil)
while let file = fileEnumerator?.nextObject() {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download