<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 8.0.0 -",
    "macOS: 10.10.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/URL",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "s:10Foundation3URLV"
  },
  "title" : "URL"
}
-->

# URL

A value that identifies the location of a resource, such as an item on a remote server or the path to a local file.

```
struct URL
```

## Overview

You can construct URLs and access their parts. For URLs that represent local files, you can also manipulate properties of those files directly, such as changing the file’s last modification date. Finally, you can pass URLs to other APIs to retrieve the contents of those URLs. For example, you can use [`URLSession`](/documentation/Foundation/URLSession) and its related classes to access the contents of remote resources.

URLs are the preferred way to refer to local files. Most objects that read data from or write data to a file have methods that accept a URL instead of a pathname as the file reference. For example, you can get the contents of a local file URL as <doc://com.apple.documentation/documentation/Swift/String> by calling <doc://com.apple.documentation/documentation/Swift/String/init(contentsOf:encoding:)>, or as a [`Data`](/documentation/Foundation/Data) by calling [`init(contentsOf:options:)`](/documentation/Foundation/Data/init(contentsOf:options:)).

As a convenience, you can use Swift’s `async`-`await` syntax to asynchronously access the contents of a [`URL`](/documentation/Foundation/URL) through the [`resourceBytes`](/documentation/Foundation/URL/resourceBytes) and [`lines`](/documentation/Foundation/URL/lines) properties. These properties use the shared [`URLSession`](/documentation/Foundation/URLSession) instance to load the resource.

`URL` defines a set of properties for common directories like [`documentsDirectory`](/documentation/Foundation/URL/documentsDirectory) and [`cachesDirectory`](/documentation/Foundation/URL/cachesDirectory), some of which have distinct behaviors for backup or automatic purging. To make the best use of these directories, see [Using the file system effectively](/documentation/Foundation/using-the-file-system-effectively).

## Topics

### Creating a URL from a string

[`init(string:)`](/documentation/Foundation/URL/init(string:))

Creates a URL instance from the provided string.

[`init(string:encodingInvalidCharacters:)`](/documentation/Foundation/URL/init(string:encodingInvalidCharacters:))

Creates a URL instance from the provided string, optionally IDNA- and percent-encoding any invalid characters.

[`init(string:relativeTo:)`](/documentation/Foundation/URL/init(string:relativeTo:))

Creates a URL instance from the provided string, relative to another URL.

[`init(resolvingBookmarkData:options:relativeTo:bookmarkDataIsStale:)`](/documentation/Foundation/URL/init(resolvingBookmarkData:options:relativeTo:bookmarkDataIsStale:)-3ic6f)

Creates a URL that refers to a location specified by resolving bookmark data.

[`init(resolvingBookmarkData:options:relativeTo:bookmarkDataIsStale:)`](/documentation/Foundation/URL/init(resolvingBookmarkData:options:relativeTo:bookmarkDataIsStale:)-97e6x)

Initializes a URL that refers to a location specified by resolving bookmark data.

### Creating a file URL from a string path

[`init(filePath:directoryHint:relativeTo:)`](/documentation/Foundation/URL/init(filePath:directoryHint:relativeTo:))

Creates a file URL that references a path you specify as a string.

[`URL.DirectoryHint`](/documentation/Foundation/URL/DirectoryHint)

A hint to URL file APIs for handling paths that may reference directories.

[`init(fileURLWithPath:)`](/documentation/Foundation/URL/init(fileURLWithPath:))

Creates a file URL that references the local file or directory at the given path.

[`init(fileURLWithPath:isDirectory:)`](/documentation/Foundation/URL/init(fileURLWithPath:isDirectory:))

Creates a file URL that references the local file or directory at the given path.

[`init(fileURLWithPath:relativeTo:)`](/documentation/Foundation/URL/init(fileURLWithPath:relativeTo:))

Creates a file URL that references the local file or directory at the given path, relative to a base URL.

[`init(fileURLWithPath:isDirectory:relativeTo:)`](/documentation/Foundation/URL/init(fileURLWithPath:isDirectory:relativeTo:))

Creates a file URL that references the local file or directory at the given path, relative to a base URL.

[`init(fileURLWithFileSystemRepresentation:isDirectory:relativeTo:)`](/documentation/Foundation/URL/init(fileURLWithFileSystemRepresentation:isDirectory:relativeTo:))

Creates a file URL that references the local file or directory for the file system representation of the path.

[`init(fileReferenceLiteralResourceName:)`](/documentation/Foundation/URL/init(fileReferenceLiteralResourceName:))

Creates a URL from a playground file literal.

[`init(filePath:directoryHint:)`](/documentation/Foundation/URL/init(filePath:directoryHint:))

Creates a file URL that references a file path.

### Creating a file URL from a file path

[`init(_:)`](/documentation/Foundation/URL/init(_:))

Creates a file URL that references the local file or directory at the file path you specify.

[`init(_:isDirectory:)`](/documentation/Foundation/URL/init(_:isDirectory:))

Creates a file URL that references the local file or directory at the file path you specify.

  <doc://com.apple.documentation/documentation/System/FilePath>

### Creating a file URL for a common directory

[`init(for:in:appropriateFor:create:)`](/documentation/Foundation/URL/init(for:in:appropriateFor:create:))

Creates a file URL for a common directory in a domain.

[`FileManager.SearchPathDirectory`](/documentation/Foundation/FileManager/SearchPathDirectory)

The location of significant directories.

[`FileManager.SearchPathDomainMask`](/documentation/Foundation/FileManager/SearchPathDomainMask)

Domain constants specifying base locations to use when you search for significant directories.

### Creating a URL by resolving a bookmark

[`init(resolvingBookmarkData:options:relativeTo:bookmarkDataIsStale:)`](/documentation/Foundation/URL/init(resolvingBookmarkData:options:relativeTo:bookmarkDataIsStale:)-3ic6f)

Creates a URL that refers to a location specified by resolving bookmark data.

[`init(resolvingAliasFileAt:options:)`](/documentation/Foundation/URL/init(resolvingAliasFileAt:options:))

Creates a URL that refers to the location specified by resolving an alias file.

[`URL.BookmarkResolutionOptions`](/documentation/Foundation/URL/BookmarkResolutionOptions)

An alias for the bookmark resolution options type.

[`NSURL.BookmarkResolutionOptions`](/documentation/Foundation/NSURL/BookmarkResolutionOptions)

Options used when resolving bookmark data.

### Creating a URL from a resource

[`init(resource:)`](/documentation/Foundation/URL/init(resource:))

Creates a URL from a resource.

### Creating a URL by parsing

[`init(_:strategy:)`](/documentation/Foundation/URL/init(_:strategy:))

Creates a URL instance by parsing the provided input in accordance with a parse strategy.

[`URL.ParseStrategy`](/documentation/Foundation/URL/ParseStrategy)

A parse strategy for creating URLs from formatted strings.

### Accessing the parts of a URL

[`fragment(percentEncoded:)`](/documentation/Foundation/URL/fragment(percentEncoded:))

Returns the fragment component of the URL, optionally removing any percent-encoding.

[`fragment`](/documentation/Foundation/URL/fragment)

The fragment component of the URL if the URL conforms to RFC 3986; otherwise, nil.

[`host(percentEncoded:)`](/documentation/Foundation/URL/host(percentEncoded:))

Returns the host component of the URL, optionally removing any percent-encoding.

[`host`](/documentation/Foundation/URL/host)

The host component of a URL if the URL conforms to RFC 3986; otherwise, nil.

[`lastPathComponent`](/documentation/Foundation/URL/lastPathComponent)

The last path component of the URL, or an empty string if the path is an empty string.

[`path(percentEncoded:)`](/documentation/Foundation/URL/path(percentEncoded:))

Returns the path component of the URL, optionally removing any percent-encoding.

[`path`](/documentation/Foundation/URL/path)

The path component of the URL if the URL conforms to RFC 3986; otherwise, an empty string.

[`password(percentEncoded:)`](/documentation/Foundation/URL/password(percentEncoded:))

Returns the password component of the URL, optionally removing any percent-encoding.

[`password`](/documentation/Foundation/URL/password)

The password component of the URL if the URL conforms to RFC 3986; otherwise, nil.

[`pathComponents`](/documentation/Foundation/URL/pathComponents)

The path components of the URL, or an empty array if the path is an empty string.

[`pathExtension`](/documentation/Foundation/URL/pathExtension)

The path extension of the URL, or an empty string if the path is an empty string.

[`port`](/documentation/Foundation/URL/port)

The port component of the URL if the URL conforms to RFC 3986; otherwise, nil.

[`query(percentEncoded:)`](/documentation/Foundation/URL/query(percentEncoded:))

Returns the query component of the URL, optionally removing any percent-encoding.

[`query`](/documentation/Foundation/URL/query)

The query of the URL if the URL conforms to RFC 3986; otherwise, nil.

[`scheme`](/documentation/Foundation/URL/scheme)

The scheme of the URL.

[`user(percentEncoded:)`](/documentation/Foundation/URL/user(percentEncoded:))

Returns the user component of the URL, optionally removing any percent-encoding.

[`user`](/documentation/Foundation/URL/user)

The user component of the URL if the URL conforms to RFC 3986; otherwise, nil.

### Accessing URL representations

[`baseURL`](/documentation/Foundation/URL/baseURL)

The base URL.

[`absoluteString`](/documentation/Foundation/URL/absoluteString)

The absolute string for the URL.

[`absoluteURL`](/documentation/Foundation/URL/absoluteURL)

The absolute URL.

[`relativePath`](/documentation/Foundation/URL/relativePath)

The relative path of the URL if the URL conforms to RFC 3986, otherwise nil.

[`relativeString`](/documentation/Foundation/URL/relativeString)

The relative portion of a URL.

[`standardized`](/documentation/Foundation/URL/standardized)

A version of the URL with any instances of “..” or “.” resolved in its path.

[`standardizedFileURL`](/documentation/Foundation/URL/standardizedFileURL)

A standardized version of the path of a file URL.

### Accessing resource values

[`resourceValues(forKeys:)`](/documentation/Foundation/URL/resourceValues(forKeys:))

Returns a collection of resource values identified by the given resource keys.

[`setResourceValues(_:)`](/documentation/Foundation/URL/setResourceValues(_:))

Sets the resource value identified by a given resource key.

[`removeCachedResourceValue(forKey:)`](/documentation/Foundation/URL/removeCachedResourceValue(forKey:))

Removes the cached resource value identified by a given resource value key from the URL object.

[`removeAllCachedResourceValues()`](/documentation/Foundation/URL/removeAllCachedResourceValues())

Removes all cached resource values and all temporary resource values from the URL object.

[`setTemporaryResourceValue(_:forKey:)`](/documentation/Foundation/URL/setTemporaryResourceValue(_:forKey:))

Sets a temporary resource value on the URL object.

[`URLResourceKey`](/documentation/Foundation/URLResourceKey)

Keys that apply to file system URLs.

[`URLResourceValues`](/documentation/Foundation/URLResourceValues)

The properties that the file system resources support.

### Working with the data representation of a URL

[`init(dataRepresentation:relativeTo:isAbsolute:)`](/documentation/Foundation/URL/init(dataRepresentation:relativeTo:isAbsolute:))

Initializes a newly created URL using the contents of the given data, relative to a base URL.

[`dataRepresentation`](/documentation/Foundation/URL/dataRepresentation)

The data representation of the URL’s relativeString.

### Working with file URLs

[`isFileURL`](/documentation/Foundation/URL/isFileURL)

A Boolean that is true if the scheme is `file:`.

[`hasDirectoryPath`](/documentation/Foundation/URL/hasDirectoryPath)

A Boolean that is true if the URL path represents a directory.

[`withUnsafeFileSystemRepresentation(_:)`](/documentation/Foundation/URL/withUnsafeFileSystemRepresentation(_:))

Passes the URL’s path in the file system representation to a closure.

[`resolveSymlinksInPath()`](/documentation/Foundation/URL/resolveSymlinksInPath())

Resolves any symlinks in the path of a file URL.

[`resolvingSymlinksInPath()`](/documentation/Foundation/URL/resolvingSymlinksInPath())

Resolves any symlinks in the path of a file URL.

[`standardize()`](/documentation/Foundation/URL/standardize())

Standardizes the path of a file URL.

### Accessing common directories

[`applicationDirectory`](/documentation/Foundation/URL/applicationDirectory)

The standard directory for apps.

[`applicationSupportDirectory`](/documentation/Foundation/URL/applicationSupportDirectory)

The standard directory for application support files.

[`cachesDirectory`](/documentation/Foundation/URL/cachesDirectory)

The standard directory for discardable cache files.

[`desktopDirectory`](/documentation/Foundation/URL/desktopDirectory)

The standard directory for files on the desktop.

[`documentsDirectory`](/documentation/Foundation/URL/documentsDirectory)

The standard directory for document files.

[`downloadsDirectory`](/documentation/Foundation/URL/downloadsDirectory)

The standard directory for download files.

[`libraryDirectory`](/documentation/Foundation/URL/libraryDirectory)

The standard directory for documentation, support, and configuration files.

[`moviesDirectory`](/documentation/Foundation/URL/moviesDirectory)

The standard directory for movie files.

[`musicDirectory`](/documentation/Foundation/URL/musicDirectory)

The standard directory for music files.

[`picturesDirectory`](/documentation/Foundation/URL/picturesDirectory)

The standard directory for image files.

[`sharedPublicDirectory`](/documentation/Foundation/URL/sharedPublicDirectory)

The standard directory for publicly shared files.

[`temporaryDirectory`](/documentation/Foundation/URL/temporaryDirectory)

The standard directory for temporary files.

[`trashDirectory`](/documentation/Foundation/URL/trashDirectory)

The standard trash directory.

[`userDirectory`](/documentation/Foundation/URL/userDirectory)

The container directory of user home directories.

### Accessing home and user directories

[`currentDirectory()`](/documentation/Foundation/URL/currentDirectory())

Returns the working directory of the current process.

[`homeDirectory`](/documentation/Foundation/URL/homeDirectory)

The home directory for the current user.

[`homeDirectory(forUser:)`](/documentation/Foundation/URL/homeDirectory(forUser:))

Returns the home directory for the specified user.

### Adding path components

[`append(path:directoryHint:)`](/documentation/Foundation/URL/append(path:directoryHint:))

Appends a path to the URL, with a hint for handling directory awareness.

[`append(component:directoryHint:)`](/documentation/Foundation/URL/append(component:directoryHint:))

Appends a path component to the URL, with a hint for handling directory awareness.

[`appendPathComponent(_:)`](/documentation/Foundation/URL/appendPathComponent(_:))

Appends a path component to the URL.

[`appendPathComponent(_:isDirectory:)`](/documentation/Foundation/URL/appendPathComponent(_:isDirectory:))

Appends a path component to the URL, specifying whether the resulting path is a directory.

[`appending(path:directoryHint:)`](/documentation/Foundation/URL/appending(path:directoryHint:))

Returns a URL by appending the specified path to the URL, with a hint for handling directory awareness.

[`appending(component:directoryHint:)`](/documentation/Foundation/URL/appending(component:directoryHint:))

Returns a URL by appending the specified path component to the URL, with a hint for handling directory awareness.

[`appendingPathComponent(_:)`](/documentation/Foundation/URL/appendingPathComponent(_:))

Returns a URL by appending the specified path component to self.

[`appendingPathComponent(_:isDirectory:)`](/documentation/Foundation/URL/appendingPathComponent(_:isDirectory:))

Returns a URL by appending the specified path component to self, specifying whether the resulting path is a directory.

[`append(components:directoryHint:)`](/documentation/Foundation/URL/append(components:directoryHint:))

Appends multiple path components to the URL, with a hint for handling directory awareness.

[`appending(components:directoryHint:)`](/documentation/Foundation/URL/appending(components:directoryHint:))

Returns a new URL by appending multiple path components to the URL, with a hint for handling directory awareness.

[`appendPathComponent(_:conformingTo:)`](/documentation/Foundation/URL/appendPathComponent(_:conformingTo:))

Appends a path component to the URL that conforms to a uniform type identifier.

[`appendingPathComponent(_:conformingTo:)`](/documentation/Foundation/URL/appendingPathComponent(_:conformingTo:))

Returns a URL by appending the specified path component that conforms to a uniform type identifier.

### Adding a path extension

[`appendPathExtension(_:)`](/documentation/Foundation/URL/appendPathExtension(_:))

Appends the specified path extension to self.

[`appendingPathExtension(_:)`](/documentation/Foundation/URL/appendingPathExtension(_:))

Returns a URL by appending the specified path extension to self.

[`appendPathExtension(for:)`](/documentation/Foundation/URL/appendPathExtension(for:))

Appends the preferred path extension for the type you specify.

[`appendingPathExtension(for:)`](/documentation/Foundation/URL/appendingPathExtension(for:))

Returns a URL by appending the preferred path extension for the type you specify to the URL’s last path component.

### Adding query items

[`append(queryItems:)`](/documentation/Foundation/URL/append(queryItems:))

Appends a list of query items to the URL.

[`appending(queryItems:)`](/documentation/Foundation/URL/appending(queryItems:))

Returns a new URL formed by appending a list of query items to the URL.

[`URLQueryItem`](/documentation/Foundation/URLQueryItem)

A single name-value pair from the query portion of a URL.

### Removing path components

[`deleteLastPathComponent()`](/documentation/Foundation/URL/deleteLastPathComponent())

Returns a URL constructed by removing the last path component of self.

[`deletingLastPathComponent()`](/documentation/Foundation/URL/deletingLastPathComponent())

Returns a URL constructed by removing the last path component of self.

### Removing a path extension

[`deletePathExtension()`](/documentation/Foundation/URL/deletePathExtension())

Returns a URL constructed by removing any path extension.

[`deletingPathExtension()`](/documentation/Foundation/URL/deletingPathExtension())

Returns a URL constructed by removing any path extension.

### Creating bookmarks

[`bookmarkData(options:includingResourceValuesForKeys:relativeTo:)`](/documentation/Foundation/URL/bookmarkData(options:includingResourceValuesForKeys:relativeTo:))

Returns bookmark data for the URL, created with specified options and resource values.

[`bookmarkData(withContentsOf:)`](/documentation/Foundation/URL/bookmarkData(withContentsOf:))

Initializes and returns bookmark data derived from an alias file pointed to by a specified URL.

[`writeBookmarkData(_:to:)`](/documentation/Foundation/URL/writeBookmarkData(_:to:))

Creates an alias file on disk at a specified location with specified bookmark data.

[`resourceValues(forKeys:fromBookmarkData:)`](/documentation/Foundation/URL/resourceValues(forKeys:fromBookmarkData:))

Returns the resource values for properties identified by a specified array of keys contained in specified bookmark data.

[`URL.BookmarkCreationOptions`](/documentation/Foundation/URL/BookmarkCreationOptions)

An alias for bookmark creation options.

[`NSURL.BookmarkCreationOptions`](/documentation/Foundation/NSURL/BookmarkCreationOptions)

Options used when creating bookmark data.

### Checking reachability

[`checkResourceIsReachable()`](/documentation/Foundation/URL/checkResourceIsReachable())

Returns whether the URL’s resource exists and is reachable.

### Loading URL contents asynchronously

[`resourceBytes`](/documentation/Foundation/URL/resourceBytes)

The URL’s resource data, as an asynchronous sequence of bytes.

[`lines`](/documentation/Foundation/URL/lines)

The URL’s resource data, as an asynchronous sequence of lines of text.

[`URL.AsyncBytes`](/documentation/Foundation/URL/AsyncBytes)

An asynchronous sequence of bytes loaded from the URL.

### Working with promised items

[`checkPromisedItemIsReachable()`](/documentation/Foundation/URL/checkPromisedItemIsReachable())

Returns whether the promised item URL’s resource exists and is reachable.

[`promisedItemResourceValues(forKeys:)`](/documentation/Foundation/URL/promisedItemResourceValues(forKeys:))

Gets resource values from URLs of ‘promised’ items.

### Working with security scoped resources

[`startAccessingSecurityScopedResource()`](/documentation/Foundation/URL/startAccessingSecurityScopedResource())

Given a url created by resolving a bookmark data created with security scope, make the resource referenced by the url accessible to the process.

[`stopAccessingSecurityScopedResource()`](/documentation/Foundation/URL/stopAccessingSecurityScopedResource())

Revokes the access granted to the url by a prior successful call to the complementary start function.

### Comparing URLs

### Describing a URL

[`customPlaygroundQuickLook`](/documentation/Foundation/URL/customPlaygroundQuickLook)

A playground quicklook for the URL.

### Formatting a URL

[`formatted()`](/documentation/Foundation/URL/formatted())

Formats the URL using a default format style.

[`formatted(_:)`](/documentation/Foundation/URL/formatted(_:))

Formats the URL, using the provided format style.

[`URL.FormatStyle`](/documentation/Foundation/URL/FormatStyle)

A structure that converts between URL instances and their textual representations.

### Using reference types

[`NSURL`](/documentation/Foundation/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.

### Core Transferable support

### App Intents support

[`defaultResolverSpecification`](/documentation/Foundation/URL/defaultResolverSpecification)

The default resolver specification that the App Intents framework uses.

[`URL.Specification`](/documentation/Foundation/URL/Specification)

The specification type for conforming with App Intents.

[`URL.UnwrappedType`](/documentation/Foundation/URL/UnwrappedType)

The core type for conforming with App Intents.

[`URL.ValueType`](/documentation/Foundation/URL/ValueType)

The value type for conforming with App Intents.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)