Tuesday, April 23, 2013

NSFileManager URLForDirectory example ios


URLForDirectory :inDomain:appropriateForURL:create:error:

Locates and optionally creates the specified common directory in a domain.
- (NSURL *)URLForDirectory:(NSSearchPathDirectory)directory inDomain:(NSSearchPathDomainMask)domainappropriateForURL:(NSURL *)url create:(BOOL)shouldCreate error:(NSError **)error
Parameters
directory
The search path directory. The supported values are described in NSSearchPathDirectory.
domain
The file system domain to search. The value for this parameter is one of the constants described in NSSearchPathDomainMask. You should specify only one domain for your search and you may not specify the NSAllDomainsMask constant for this parameter.
url
The name of a directory inside of which you want to create a unique temporary directory for autosaving documents or some other use. This parameter is ignored unless the directory parameter contains the value NSItemReplacementDirectory and the domainparameter contains the value NSUserDomainMask. When creating a temporary directory, the shouldCreate parameter is ignored and the directory is always created. [NSFileManager URLForDirectory]
shouldCreate
Specify YES if you want the directory to be created if it does not exist.
error
On input, a pointer to an error object. If an error occurs, this pointer is set to an actual error object containing the error information. You may specify nil for this parameter if you do not want the error information.
Return Value
The NSURL for the requested directory or nil if an error occurred.
Discussion of [NSFileManager URLForDirectory]
You typically use this method to locate one of the standard system directories, such as the DocumentsApplication Support or Cachesdirectories. You can also use this method to create a new temporary directory for storing things like autosave files; to do so, specifyNSItemReplacementDirectory for the directory parameter, NSUserDomainMask for the domain parameter, and a valid parent directory for the url parameter. After locating (or creating) the desired directory, this method returns the URL for that directory. If more than one appropriate directory exists in the specified domain, this method returns only the first one it finds.
Passing a directory and domain pair that makes no sense (for example NSDesktopDirectory and NSNetworkDomainMask) raises an exception.
Example of [NSFileManager URLForDirectory]
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];