Class: NSURL
Overview
An NSURL object lets you manipulate URLs and the resources they reference. The URLs employed by the NSURL class are described in RFCs 1808, 1738, and 2732. The litmus test for conformance to RFC 1808 is as recommended in RFC 1808—specifically, whether the first two characters of resourceSpecifier are slashes (//).
Class Method Summary (collapse)
-
+ bookmarkDataWithContentsOfURL:error:
Initializes and returns bookmark data derived from an alias file pointed to by a specified URL.
-
+ fileURLWithPath:
Initializes and returns a newly created NSURL object as a file URL with a specified path.
-
+ fileURLWithPath:isDirectory:
Initializes and returns a newly created NSURL object as a file URL with a specified path.
-
+ fileURLWithPathComponents:
Initializes and returns a newly created NSURL object as a file URL with specified path components.
-
+ resourceValuesForKeys:fromBookmarkData:
Returns the resource values for properties identified by a specified array of keys contained in specified bookmark data.
-
+ URLByResolvingBookmarkData:options:relativeToURL:bookmarkDataIsStale:error:
Returns a new URL made by resolving bookmark data.
-
+ URLWithString:
Creates and returns an NSURL object initialized with a provided string.
-
+ URLWithString:relativeToURL:
Creates and returns an NSURL object initialized with a base URL and a relative string.
-
+ writeBookmarkData:toURL:options:error:
Creates an alias file on disk at a specified location with specified bookmark data.
Instance Method Summary (collapse)
-
- absoluteString
Returns the string for the receiver as if it were an absolute URL.
-
- absoluteURL
Returns an absolute URL that refers to the same resource as the receiver.
-
- baseURL
Returns the base URL of the receiver.
-
- bookmarkDataWithOptions:includingResourceValuesForKeys:relativeToURL:error:
Returns a bookmark for the URL, created with specified options and resource values.
-
- checkResourceIsReachableAndReturnError:
Returns whether the resource pointed to by a file URL can be reached.
-
- filePathURL
Returns a new file path URL that points to the same resource as the original URL.
-
- fileReferenceURL
Returns a new file reference URL that points to the same resource as the original URL.
-
- fragment
Returns the fragment of a URL conforming to RFC 1808.
-
- getResourceValue:forKey:error:
Returns the resource value for the property identified by a given key.
-
- host
Returns the host of a URL conforming to RFC 1808.
-
- initByResolvingBookmarkData:options:relativeToURL:bookmarkDataIsStale:error:
Initializes a newly created NSURL that points to a location specified by resolving bookmark data.
-
- initFileURLWithPath:
Initializes a newly created NSURL referencing the local file or directory at path.
-
- initFileURLWithPath:isDirectory:
Initializes a newly created NSURL referencing the local file or directory at path.
-
- initWithScheme:host:path:
Initializes a newly created NSURL with a specified scheme, host, and path.
-
- initWithString:
Initializes an NSURL object with a provided string.
-
- initWithString:relativeToURL:
Initializes an NSURL object with a base URL and a relative string.
-
- isEqual:
Returns a Boolean value that indicates whether the receiver and a given object are equal.
-
- isFileReferenceURL
Returns whether the URL is a file reference URL.
-
- isFileURL
Returns whether the receiver uses the file scheme.
-
- lastPathComponent
Returns the last path component of a file URL.
-
- parameterString
Returns the parameter string of a URL conforming to RFC 1808.
-
- password
Returns the password of a URL conforming to RFC 1808.
-
- path
Returns the path of a URL conforming to RFC 1808.
-
- pathComponents
Returns the individual path components of a file URL in an array.
-
- pathExtension
Returns the path extension of a file URL.
-
- port
Returns the port number of a URL conforming to RFC 1808.
-
- query
Returns the query of a URL conforming to RFC 1808.
-
- relativePath
Returns the path of a URL conforming to RFC 1808, without resolving against the receiver’s base URL.
-
- relativeString
Returns a string representation of the relative portion of the URL.
-
- resourceSpecifier
Returns the resource specifier of the URL.
-
- resourceValuesForKeys:error:
Returns the resource values for the properties identified by specified array of keys.
-
- scheme
Returns the scheme of the URL.
-
- setResourceValue:forKey:error:
Sets the resource property of the URL specified by a given key to a given value.
-
- setResourceValues:error:
Sets resource properties of the URL specified by a given set of keys to a given set of values.
-
- standardizedURL
Returns a new NSURL object with any instances of ".." or "." removed from its path.
-
- URLByAppendingPathComponent:
Returns a new URL made by appending a path component to the original URL.
-
- URLByAppendingPathComponent::
Returns a new URL made by appending a path component to the original URL, along with a trailing slash if the component is designated a directory.
-
- URLByAppendingPathExtension:
Returns a new URL made by appending a path extension to the original URL.
-
- URLByDeletingLastPathComponent
Returns a new URL made by deleting the last path component from the original URL.
-
- URLByDeletingPathExtension
Returns a new URL made by deleting the path extension, if any, from the original URL.
-
- URLByResolvingSymlinksInPath
Returns a new URL that points to the same resource as the original URL and includes no symbolic links.
-
- URLByStandardizingPath
Returns a new URL that points to the same resource as the original URL and is an absolute path.
-
- user
Returns the user portion of a URL conforming to RFC 1808.
Methods inherited from NSObject
#!, #!=, #!~, #, #==, #===, #=~, #Rational, #__callee__, #__method__, #__send__, #__type__, `, alloc, allocWithZone:, #autoContentAccessingProxy, autoload, autoload?, autorelease_pool, #awakeAfterUsingCoder:, binding, block_given?, caller, cancelPreviousPerformRequestsWithTarget:, cancelPreviousPerformRequestsWithTarget:selector:object:, catch, class, classFallbacksForKeyedArchiver, #classForCoder, #classForKeyedArchiver, classForKeyedUnarchiver, #clone, conformsToProtocol:, #copy, copyWithZone:, #dealloc, #define_singleton_method, description, display, #doesNotRecognizeSelector:, #dup, #enum_for, #eql?, #equal?, #extend, fail, #finalize, format, #forwardInvocation:, #forwardingTargetForSelector:, framework, #freeze, #frozen?, getpass, gets, global_variables, #init, initialize, #initialize_clone, #initialize_copy, #initialize_dup, #inspect, instanceMethodForSelector:, instanceMethodSignatureForSelector:, #instance_eval, #instance_exec, #instance_of?, #instance_variable_defined?, #instance_variable_get, #instance_variable_set, #instance_variables, instancesRespondToSelector:, isSubclassOfClass:, #is_a?, iterator?, #kind_of?, lambda, load, load_bridge_support_file, load_plist, local_variables, loop, #method, #methodForSelector:, #methodSignatureForSelector:, #methods, #mutableCopy, mutableCopyWithZone:, new, #nil?, open, p, #performSelector:onThread:withObject:waitUntilDone:, #performSelector:onThread:withObject:waitUntilDone:modes:, #performSelector:withObject:afterDelay:, #performSelector:withObject:afterDelay:inModes:, #performSelectorInBackground:withObject:, #performSelectorOnMainThread:withObject:waitUntilDone:, #performSelectorOnMainThread:withObject:waitUntilDone:modes:, print, printf, #private_methods, proc, #protected_methods, #public_method, #public_methods, #public_send, putc, puts, raise, rand, readline, readlines, #replacementObjectForCoder:, #replacementObjectForKeyedArchiver:, require, resolveClassMethod:, resolveInstanceMethod:, #respond_to?, #respond_to_missing?, select, #send, setVersion:, #singleton_methods, sprintf, srand, superclass, #taint, #tainted?, #tap, test, throw, #to_plist, #to_s, trace_var, trap, #trust, #untaint, untrace_var, #untrust, #untrusted?, version
Constructor Details
This class inherits a constructor from NSObject
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class NSObject
Class Method Details
+ (NSData) bookmarkDataWithContentsOfURL(bookmarkFileURL, error:error)
Initializes and returns bookmark data derived from an alias file pointed to by a specified URL. If bookmarkFileURL points to an alias file created prior to OS X v10.6 that contains Alias Manager information but no bookmark data, this method synthesizes bookmark data for the file.This method returns nil if bookmark data cannot be created.
+ (Object) fileURLWithPath(path)
Initializes and returns a newly created NSURL object as a file URL with a specified path. This method assumes that path is a directory if it ends with a slash. If path does not end with a slash, the method examines the file system to determine if path is a file or a directory. If path exists in the file system and is a directory, the method appends a trailing slash. If path does not exist in the file system, the method assumes that it represents a file and does not append a trailing slash.As an alternative, consider using fileURLWithPath:isDirectory:, which allows you to explicitly specify whether the returned NSURL object represents a file or directory.
+ (Object) fileURLWithPath(path, isDirectory:isDir)
Initializes and returns a newly created NSURL object as a file URL with a specified path.
+ (NSURL) fileURLWithPathComponents(components)
Initializes and returns a newly created NSURL object as a file URL with specified path components. The path components are separated by a forward slash in the returned URL.
+ (Hash) resourceValuesForKeys(keys, fromBookmarkData:bookmarkData)
Returns the resource values for properties identified by a specified array of keys contained in specified bookmark data.
+ (Object) URLByResolvingBookmarkData(bookmarkData, options:options, relativeToURL:relativeURL, bookmarkDataIsStale:isStale, error:error)
Returns a new URL made by resolving bookmark data. To obtain a security-scoped URL from a security-scoped bookmark, call this method using the NSURLBookmarkResolutionWithSecurityScope option. In addition, to use security scope, you must first have enabled the appropriate entitlements for your app, as described in “Enabling Security-Scoped Bookmark and URL Access” in Entitlement Key Reference.To then obtain access to the file-system resource pointed to by a security-scoped URL (in other words, to bring the resource into your app’s sandbox), call the startAccessingSecurityScopedResource method (or its Core Foundation equivalent) on the URL.For an app-scoped bookmark, no sandboxed app other than the one that created the bookmark can obtain access to the file-system resource that the URL (obtained from the bookmark) points to.For a document-scoped bookmark, any sandboxed app that has access to the bookmark data itself, and has access to the document that owns the bookmark, can obtain access to the resource.Version note: Security-scoped bookmarks are not available in versions of OS X prior to OS X v10.7.3.
+ (Object) URLWithString(URLString)
Creates and returns an NSURL object initialized with a provided string. This method expects URLString to contain any necessary percent escape codes, which are ‘:’, ‘/’, ‘%’, ‘#’, ‘;’, and ‘@’. Note that ‘%’ escapes are translated via UTF-8.
+ (Object) URLWithString(URLString, relativeToURL:baseURL)
Creates and returns an NSURL object initialized with a base URL and a relative string. This method expects URLString to contain any necessary percent escape codes.
+ (Boolean) writeBookmarkData(bookmarkData, toURL:bookmarkFileURL, options:options, error:error)
Creates an alias file on disk at a specified location with specified bookmark data. This method will produce an error if bookmarkData was not created with the NSURLBookmarkCreationSuitableForBookmarkFile option.If bookmarkFileURL points to a directory, the alias file will be created in that directory with its name derived from the information in bookmarkData. If bookmarkFileURL points to a file, the alias file will be created with the location and name indicated by bookmarkFileURL, and its extension will be changed to .alias if it is not already.
Instance Method Details
- (String) absoluteString
Returns the string for the receiver as if it were an absolute URL.
- (NSURL) absoluteURL
Returns an absolute URL that refers to the same resource as the receiver.
- (NSURL) baseURL
Returns the base URL of the receiver.
- (NSData) bookmarkDataWithOptions(options, includingResourceValuesForKeys:keys, relativeToURL:relativeURL, error:error)
Returns a bookmark for the URL, created with specified options and resource values. To use this method to create a security-scoped bookmark to support App Sandbox, you must first have enabled the appropriate entitlements for your app, as described in “Enabling Security-Scoped Bookmark and URL Access” in Entitlement Key Reference. In addition, be sure to understand the behavior of the options and relativeURL parameters.For an app-scoped bookmark, no sandboxed app other than the one that created the bookmark can obtain access to the file-system resource that the URL (obtained from the bookmark) points to. Specifically, a bookmark created with security scope fails to resolve if the caller does not have the same code signing identity as the caller that created the bookmark.For a document-scoped bookmark, any sandboxed app that has access to the bookmark data itself, and has access to the document that owns the bookmark, can obtain access to the resource.Version note: Security-scoped bookmarks are not available in versions of OS X prior to OS X v10.7.3.
- (Boolean) checkResourceIsReachableAndReturnError(error)
Returns whether the resource pointed to by a file URL can be reached.
- (NSURL) filePathURL
Returns a new file path URL that points to the same resource as the original URL. If the original URL is a file reference URL, this method converts it to a file path URL. If the original URL is a file path URL, the returned URL is identical. If the original URL is not a file URL, this method returns nil.
- (NSURL) fileReferenceURL
Returns a new file reference URL that points to the same resource as the original URL. If the original URL is a file path URL, this method converts it to a file reference URL. If the original URL is a file reference URL, the returned URL is identical. If the original URL is not a file URL, this method returns nil.
- (String) fragment
Returns the fragment of a URL conforming to RFC 1808.
- (Boolean) getResourceValue(value, forKey:key, error:error)
Returns the resource value for the property identified by a given key. value is set to nil if the requested resource value is not defined for the URL. In this case, the method still returns YES.
- (String) host
Returns the host of a URL conforming to RFC 1808.
- (Object) initByResolvingBookmarkData(bookmarkData, options:options, relativeToURL:relativeURL, bookmarkDataIsStale:isStale, error:error)
Initializes a newly created NSURL that points to a location specified by resolving bookmark data.
- (Object) initFileURLWithPath(path)
Initializes a newly created NSURL referencing the local file or directory at path. Invoking this method is equivalent to invoking initWithScheme:host:path: with scheme NSURLFileScheme, a nil host, and path.This method examines path in the file system to determine if it is a directory. If path is a directory, then a trailing slash is appended. If the file does not exist, it is assumed that path represents a directory and a trailing slash is appended. As an alternative, consider using initFileURLWithPath:isDirectory: which allows you to explicitly specify whether the returned NSURL represents a file or directory.
- (Object) initFileURLWithPath(path, isDirectory:isDir)
Initializes a newly created NSURL referencing the local file or directory at path. Invoking this method is equivalent to invoking initWithScheme:host:path: with scheme NSURLFileScheme, a nil host, and path.
- (Object) initWithScheme(scheme, host:host, path:path)
Initializes a newly created NSURL with a specified scheme, host, and path. This method automatically escapes path with the stringByAddingPercentEscapesUsingEncoding: method.
- (Object) initWithString(URLString)
Initializes an NSURL object with a provided string. This method expects URLString to contain any necessary percent escape codes, which are ‘:’, ‘/’, ‘%’, ‘#’, ‘;’, and ‘@’. Note that ‘%’ escapes are translated via UTF-8.
- (Object) initWithString(URLString, relativeToURL:baseURL)
- (Boolean) isEqual(anObject)
Returns a Boolean value that indicates whether the receiver and a given object are equal. This method defines what it means for instances to be equal. Two NSURLs are considered equal if and only if they return identical values for both baseURL and relativeString.
- (Boolean) isFileReferenceURL
Returns whether the URL is a file reference URL.
- (Boolean) isFileURL
Returns whether the receiver uses the file scheme.
- (String) lastPathComponent
Returns the last path component of a file URL.
- (String) parameterString
Returns the parameter string of a URL conforming to RFC 1808.
- (String) password
Returns the password of a URL conforming to RFC 1808.
- (String) path
Returns the path of a URL conforming to RFC 1808.
- (Array) pathComponents
Returns the individual path components of a file URL in an array.
- (String) pathExtension
Returns the path extension of a file URL.
- (NSNumber) port
Returns the port number of a URL conforming to RFC 1808.
- (String) query
Returns the query of a URL conforming to RFC 1808.
- (String) relativePath
Returns the path of a URL conforming to RFC 1808, without resolving against the receiver’s base URL.
- (String) relativeString
Returns a string representation of the relative portion of the URL.
- (String) resourceSpecifier
Returns the resource specifier of the URL.
- (Hash) resourceValuesForKeys(keys, error:error)
Returns the resource values for the properties identified by specified array of keys. If an error occurs, this method returns nil. A key is left out of the returned dictionary if its corresponding resource value is not defined for the URL.
- (String) scheme
Returns the scheme of the URL.
- (Boolean) setResourceValue(value, forKey:key, error:error)
Sets the resource property of the URL specified by a given key to a given value. The resource is modified synchronously.
- (Boolean) setResourceValues(keyedValues, error:error)
Sets resource properties of the URL specified by a given set of keys to a given set of values. If an error occurs during the execution of this method, error will contain an array of the resource values that were not successfully set in its userInfo dictionary.
- (NSURL) standardizedURL
Returns a new NSURL object with any instances of ".." or "." removed from its path.
- (NSURL) URLByAppendingPathComponent(pathComponent)
Returns a new URL made by appending a path component to the original URL. If the original URL does not end with a forward slash and pathComponent does not begin with a forward slash, a forward slash is inserted between the two parts of the returned URL, unless the original URL is the empty string.
- (NSURL) URLByAppendingPathComponent(pathComponentisDirectory, :isDirectory)
Returns a new URL made by appending a path component to the original URL, along with a trailing slash if the component is designated a directory. If the original URL does not end with a forward slash and pathComponent does not begin with a forward slash, a forward slash is inserted between the two parts of the returned URL, unless the original URL is the empty string.
- (NSURL) URLByAppendingPathExtension(pathExtension)
Returns a new URL made by appending a path extension to the original URL. If the original URL ends with one or more forward slashes, these are removed from the returned URL. A period is inserted between the two parts of the new URL.
- (NSURL) URLByDeletingLastPathComponent
Returns a new URL made by deleting the last path component from the original URL. If the original URL represents the root path, the returned URL is identical. Otherwise, if the original URL has only one path component, the new URL is the empty string.
- (NSURL) URLByDeletingPathExtension
Returns a new URL made by deleting the path extension, if any, from the original URL. If the original URL represents the root path, the returned URL is identical. If the URL has multiple path extensions, only the last one is removed.
- (NSURL) URLByResolvingSymlinksInPath
Returns a new URL that points to the same resource as the original URL and includes no symbolic links. If the original URL has no symbolic links, the returned URL is identical to the original URL.This method only works on URLs with the file: path scheme. This method will return an identical URL for all other URLs.
- (NSURL) URLByStandardizingPath
Returns a new URL that points to the same resource as the original URL and is an absolute path. This method only works on URLs with the file: path scheme. This method will return an identical URL for all other URLs.
- (String) user
Returns the user portion of a URL conforming to RFC 1808.