<!--
{
  "availability" : [
    "iOS: 16.0.0 -",
    "iPadOS: 16.0.0 -",
    "macCatalyst: 16.0.0 -",
    "macOS: 13.0.0 -",
    "tvOS: 16.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 9.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/URL/host(percentEncoded:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "s:10Foundation3URLV4host14percentEncodedSSSgSb_tF"
  },
  "title" : "host(percentEncoded:)"
}
-->

# host(percentEncoded:)

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

```
func host(percentEncoded: Bool = true) -> String?
```

## Parameters

`percentEncoded`

A Boolean value that indicates whether the URL percent-encodes any unreserved characters. Defaults to `true`.

## Return Value

The fragment component of the URL, optionally percent-encoding any unreserved characters.

## Discussion

The system doesn’t allow certain characters in the URL host component, so [`URL`](/documentation/Foundation/URL) percent-encodes those characters to create a valid URL. Calling this function with `percentEncoded = false` removes any percent-encoding and returns the unencoded host.

If the URL doesn’t contain a host component according to [RFC 3986](https://www.ietf.org/rfc/rfc3986.txt), this function returns `nil`.

---

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)