NSFileManager - Swift - File Browser

I am new to ios development and I am trying to create an application in swift that displays all the files in a directory (all files will be PDFs) and the user can open them.

I have googling this for the last two days and am very confused. Can someone suggest a tutorial or steps that I would need for this to work.

I started with this in my ViewController.swift file:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    class func defaultManager()->NSFileManager{

    }

}

I just don’t know what to do next, it’s very sad, I know. I would appreciate any help or help.

Thanks J

+4
source share
3 answers
let manager = NSFileManager.defaultManager()
var array = manager.contentsOfDirectoryAtPath(_ path: String,
                         error error: NSErrorPointer) -> [AnyObject]?

Swift version 3.0

let manager = FileManager.default
let installed_files = try manager.contentsOfDirectory(atPath: "/Applications/")
+4
source

Here is a more complete example and updated for Swift 2.

( ) . , .

private func getListOfFileNames() -> Array<String> {

    let docsPath = NSBundle.mainBundle().resourcePath! + "/DirectoryName"
    let fileManager = NSFileManager.defaultManager()
    let docsArray: Array<String>

    do {
        docsArray = try fileManager.contentsOfDirectoryAtPath(docsPath)
    } catch {
        print(error)
    }

    return docsArray
}
+2

I may be late to answer for you, but the answer may help others. I found one library on github . This is a very easy way to access file browsing. Here is an example:

let fileBrowser = FileBrowser()
present(fileBrowser, animated: true, completion: nil)
+2
source

All Articles