Class

NSURLComponents

An object that parses URLs into and constructs URLs from their constituent parts.

Declaration

@interface NSURLComponents : NSObject

Overview

The NSURLComponents class is a class that is designed to parse URLs based on RFC 3986 and to construct URLs from their constituent parts. Its behavior differs subtly from the NSURL class, which conforms to older RFCs. However, you can easily obtain an NSURL object based on the contents of a URL components object or vice versa.

You create a URL components object in one of three ways: from an NSString object that contains a URL, from an NSURL object, or from scratch by using the default initializer. From there, you can modify the URL’s individual components and subcomponents by modifying various properties, either in unencoded form or in URL-encoded form. If you set the unencoded property, you can then obtain the encoded equivalent by reading the encoded property value and vice versa.

Topics

Creating URL Components

+ componentsWithString:

Returns a URL components object by parsing a URL in string form.

+ componentsWithURL:resolvingAgainstBaseURL:

Returns a URL components object by parsing the URL from an NSURL object.

- init

Initializes a URL components object with nil for every component.

- initWithString:

Initializes a URL components object by parsing a URL in string form.

- initWithURL:resolvingAgainstBaseURL:

Initializes a URL components object by parsing the URL from an NSURL object.

Getting the URL

string

A URL derived from the components object, in string form.

URL

A URL object derived from the components object.

- URLRelativeToURL:

Returns a URL object derived from the components object.

Accessing Components in Native Format

fragment

The fragment URL component (the part after a # symbol), or nil if not present.

host

The host URL subcomponent, or nil if not present.

password

The password URL subcomponent, or nil if not present.

path

The path URL component, or nil if not present.

port

The port number URL component, or nil if not present.

query

The query URL component as a string, or nil if not present.

queryItems

The query URL component as an array of name/value pairs.

scheme

The scheme URL component, or nil if not present.

user

The username URL subcomponent, or nil if not present.

Accessing Components in URL-Encoded Format

percentEncodedFragment

The fragment URL component (the part after a # symbol) expressed as a URL-encoded string, or nil if not present.

percentEncodedHost

The host URL subcomponent expressed as a URL-encoded string, or nil if not present.

percentEncodedPassword

The password URL subcomponent expressed as a URL-encoded string, or nil if not present.

percentEncodedPath

The path URL component expressed as a URL-encoded string, or nil if not present.

percentEncodedQuery

The query URL component expressed as a URL-encoded string, or nil if not present.

percentEncodedUser

The username URL subcomponent expressed as a URL-encoded string, or nil if not present.

Locating Components in the URL String Representation

rangeOfFragment

Returns the character range of the fragment in the string returned by the string property.

rangeOfHost

Returns the character range of the host in the string returned by the string property.

rangeOfPassword

Returns the character range of the password in the string returned by the string property.

rangeOfPath

Returns the character range of the path in the string returned by the string property.

rangeOfPort

Returns the character range of the port in the string returned by the string property.

rangeOfQuery

Returns the character range of the query in the string returned by the string property.

rangeOfScheme

Returns the character range of the scheme in the string returned by the string property.

rangeOfUser

Returns the character range of the user in the string returned by the string property.

Relationships

Inherits From

Conforms To

See Also

URLs

NSURL

An object that represents the location of a resource, such as an item on a remote server or the path to a local file.

NSURLQueryItem

An object representing a single name/value pair for an item in the query portion of a URL.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software