Tuesday, April 23, 2013

NSFileManager URLsForDirectory example ios


URLsForDirectory :inDomains:

Returns an array of URLs for the specified common directory in the requested domains.
- (NSArray *)URLsForDirectory:(NSSearchPathDirectory)directory inDomains:(NSSearchPathDomainMask)domainMask
Parameters
directory
The search path directory. The supported values are described in NSSearchPathDirectory.
domainMask
The file system domain to search. The value for this parameter is one or more of the constants described inNSSearchPathDomainMask.
Return Value of [NSFileManager URLsForDirectory]
An array of NSURL objects identifying the requested directories. The directories are ordered according to the order of the domain mask constants, with items in the user domain first and items in the system domain last.
Discussion of [NSFileManager URLsForDirectory]
This method is intended to locate known and common directories in the system. For example, setting the directory toNSApplicationDirectory, will return the Applications directories in the requested domain. There are a number of common directories available in the NSSearchPathDirectory, including: NSDesktopDirectoryNSApplicationSupportDirectory, and many more.

Example of [NSFileManager URLsForDirectory]

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = paths[0];
This is similar to doing:
NSArray *URLs = [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
NSURL *documentsURL = URLs[0];
The key difference is the first gives you the path as an NSString while the second gives you the path as an NSURL.
The other method can be used by doing:
NSURL *documentsURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];