<!--
{
  "availability" : [
    "iOS: 17.0.0 -",
    "iPadOS: 17.0.0 -",
    "macCatalyst: 17.0.0 -",
    "macOS: 14.0.0 -",
    "tvOS: 17.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 10.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSURLComponents/componentsWithString:encodingInvalidCharacters:",
  "metadataVersion" : "0.1.0",
  "role" : "Type Method",
  "symbol" : {
    "kind" : "Type Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSURLComponents(cm)componentsWithString:encodingInvalidCharacters:"
  },
  "title" : "componentsWithString:encodingInvalidCharacters:"
}
-->

# componentsWithString:encodingInvalidCharacters:

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

```
+ (instancetype) componentsWithString:(NSString *) URLString encodingInvalidCharacters:(BOOL) encodingInvalidCharacters;
```

## Parameters

`URLString`

The URL string to parse.

`encodingInvalidCharacters`

A Boolean value that indicates whether the initializer attempts to encode any invalid characters in `URLString`.

## Return Value

A URL components instance from the provided string, optionally with invalid characters percent-encoded.

## Discussion

If `encodingInvalidCharacters` is `true`, this initializer tries to encode the string to create a valid URL. If the URL string is still invalid after encoding, the method 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)