Can someone explain to me what the documents directory is on an iOS app and when to use it?
Here is what I believe at present:
To me, it seems to be a central folder where the user can store any files needed for the app.
This would be a different location than where Core Data stores its data?
It seems like each app gets its own documents directory.
I am free to create a subdirectory of the documents directory, like documents directory/images, or documents directory/videos?
You can access documents directory using this code it is basically used for storing file in plist format:
Here's a useful little function, which makes using/creating iOS folders a little easier.
You pass it the name of a subfolder, it'll return the full path back to you, and make sure the directory exists.
(Personally, I stick this static function in my AppDelete class, but perhaps this isn't the smartest place to put it.)
Here's how you would call it, to get the "full path" of a MySavedImages subdirectory:
And here's the full function:
Using this little function, it's easy to create a directory in your app's Documents directory (if it doesn't already exist), and to write a file into it.
Here's how I would create the directory, and write the contents of one of my image files into it:
Hope this helps !
It can be cleaner to add an extension to FileManager for this kind of awkward call, for tidiness if nothing else. Something like:
Swift 3 and 4 as global var:
As FileManager extension:
I couldn't find the code in the doc suggested by the accepted answer but I found the updated equivalent here:
File System Programming Guide :: Accessing Files and Directories »
It discourages use of NSSearchPathForDirectoriesInDomain:
Here are some other useful directory constants to play with. No doubt not all of these are supported in iOS. Also you can use the NSHomeDirectory() function which:
From NSPathUtilities.h
And finally, some convenience methods in an NSURL category http://club15cc.com/code/ios/easy-ios-file-directory-paths-with-this-handy-nsurl-category
This has changed in iOS 8. See the following tech note: https://developer.apple.com/library/ios/technotes/tn2406/_index.html
The Apple sanctioned way (from the link above) is as follows: