How to Access the IOS Documents Folder

Hey Devs,


I'm trying to access the documents folder in my IOS app so I can assign the .mov and jpeg files to a UIImage and a AVPlayer, but I can't find a way to do this. I'm very new to this so its probably me. After a LOT of research I've found FileManager, NSHomeDirectory, and Bundle.Main. I simply don't understand how to use them though. I need to be able to rename the files as well. How do I do this, or rather what is the best way to do this???

Accepted Reply

This thread has been deleted

Except that the ‘blessed’ answer uses

NSSearchPathForDirectoriesInDomains
, which I recommend against because it’s best to stay in “URL space” (that is, avoid string paths) wherever possible. I recommend this:
let docDir = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Replies

Seen this SO thread for starters?


http://stackoverflow.com/questions/35158215/rename-file-in-documentdirectory

Uses this:-

let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]

Uses …

NSSearchPathForDirectoriesInDomains

You can use that but I recommend against it; when dealing with the locations of items on disk, it’s better to use file URLs rather than file paths.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thanks for all the replys so quickly guys. I'm testing everything still, but ESKIMO, why do you not recommend using strings but URLS? Just trying to learn here.

why do you not recommend using strings but URLS?

Back in the Mac OS X 10.5 days we made a concerted effort to move Cocoa from identifying file system objects by path to identifying them by URL. At the time the impetus was the introduction of “file system reference URLs”, which provided facilities for folks moving from the traditional Mac OS File Manager (I’m talking Mac OS 9 here, not Cocoa’s NSFileManager, which is exposed to Swift as

FileManager
). Obviously that effort is irrelevant to folks working on iOS, but it turned out that the effort wasn’t wasted because other changes have piggybacked on top of it.

One key point here is that URLs can carry state beyond what’s feasible for a simple string, and that facilitates a number of cool features:

  • The URL resource APIs support caching. So, for example, when you call

    FileManager.default.contentsOfDirectory(at:includingPropertiesForKeys:options:)
    you pass in a set of resource keys you intend to look at. The resulting URLs will have those keys cached so that you can access them quickly via
    resourceValues(forKeys:)
    . The end result is a very efficient use of the file system, where the
    contentsOfDirectory(…)
    call results in a single bulk file system operation (
    getattrlistbulk
    ) that lists the directory and returns the requested attributes for each item. Neat-o!
  • URLs can carry around security information, which is the mechanism underlying the OS’s ability to give you access to items outside of your container (see

    startAccessingSecurityScopedResource()
    and
    stopAccessingSecurityScopedResource()
    ).

A second point is that URLs make it easier to draw a line between a file system location and general text. For example, if you have a string you have to remember whether that string is a path (that is, a file system location) or a general string (which you might happen to use as a file name). OTOH, using URLs for file system locations makes that very clear [1].

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

[1] You could argue that we should use a separate type for file system URLs and non-file system URLs, and I generally support that position. However, in the absence of such a distinction URLs are still better than paths.

Dear eskimo,


Thanks so much for your reply. I've been trying to use contentsOfDirectory of docDir to access the MOV files in the documents folder. However it won't work. Here's my script:


  • let docDir = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
  • let filePath = try! FileManager.default.contentsOfDirectory(at: docDir, includingPropertiesForKeys: nil)


  • I get the error message "Cannot use instance member 'docDir' within property initializer; propery initializers run before 'self' is available" in my filePath variable. Why am I getting this?

    Figured it out. The "filePath" variable had to be stated in a function. Here's the solution:


      let docDir = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
       
       
        func getFile()
        {      
            let filePath = try! FileManager.default.contentsOfDirectory(at: docDir, includingPropertiesForKeys: nil)      
            print (filePath[0])     //debugging
           
        }