Class

NSURLComponents

An object that parses URLs into and constructs URLs from their constituent parts that bridges to URLComponents; use NSURLComponents when you need reference semantics or other Foundation-specific behavior.

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

init()

Initializes a URL components object with nil for every component.

init?(string: String)

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

init?(url: URL, resolvingAgainstBaseURL: Bool)

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

Getting the URL

var string: String?

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

var url: URL?

A URL object derived from the components object.

func url(relativeTo: URL?)

Returns a URL object derived from the components object.

Accessing Components in Native Format

var fragment: String?

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

var host: String?

The host URL subcomponent, or nil if not present.

var password: String?

The password URL subcomponent, or nil if not present.

var path: String?

The path URL component, or nil if not present.

var port: NSNumber?

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

var query: String?

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

var queryItems: [URLQueryItem]?

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

var scheme: String?

The scheme URL component, or nil if not present.

var user: String?

The username URL subcomponent, or nil if not present.

Accessing Components in URL-Encoded Format

var percentEncodedFragment: String?

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

var percentEncodedHost: String?

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

var percentEncodedPassword: String?

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

var percentEncodedPath: String?

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

var percentEncodedQuery: String?

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

var percentEncodedUser: String?

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

Locating Components in the URL String Representation

var rangeOfFragment: NSRange

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

var rangeOfHost: NSRange

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

var rangeOfPassword: NSRange

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

var rangeOfPath: NSRange

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

var rangeOfPort: NSRange

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

var rangeOfQuery: NSRange

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

var rangeOfScheme: NSRange

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

var rangeOfUser: NSRange

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

Relationships

Inherits From

See Also

Using Reference Types

typealias URLComponents.ReferenceType

An alias for this value type's equivalent reference type.

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