Instance Method

URLForDirectory:inDomain:appropriateForURL:create:error:

Locates and optionally creates the specified common directory in a domain.

Declaration

- (NSURL *)URLForDirectory:(NSSearchPathDirectory)directory inDomain:(NSSearchPathDomainMask)domain appropriateForURL:(NSURL *)url create:(BOOL)shouldCreate error:(NSError * _Nullable *)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 file URL used to determine the location of the returned URL. Only the volume of this parameter is used.

This parameter is ignored unless the directory parameter contains the value NSItemReplacementDirectory and the domain parameter contains the value NSUserDomainMask.

shouldCreate

Whether to create the directory if it does not already exist.

When creating a temporary directory, this parameter is ignored and the directory is always created.

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. If an error occurs, this method returns nil and assigns an appropriate error object to the error parameter.

Discussion

You typically use this method to locate one of the standard system directories, such as the Documents, Application Support or Caches directories. 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.

You can use this method to create a new temporary directory. To do so, specify NSItemReplacementDirectory for the directory parameter, NSUserDomainMask for the domain parameter, and a URL for the url parameter which determines the volume of the returned URL.

For example, the following code results in temporaryDirectoryURL with a value in the form of /private/var/folders/d0/h37cw8ns3h1bfr_2gnwq2yyc0000gn/T/TemporaryItems/Untitled/:

NSURL *desktopURL = [NSURL fileURLWithPath:@"/Users/jappleseed/Desktop/"
                               isDirectory:YES];
NSError *error = nil;
 
NSURL *temporaryDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSItemReplacementDirectory
                                                                      inDomain:NSUserDomainMask
                                                             appropriateForURL:parentURL
                                                                        create:YES
                                                                         error:&error];
 
if (error) {
  // handle the error
}

See Also

Locating System Directories

URLsForDirectory:inDomains:

Returns an array of URLs for the specified common directory in the requested domains.