---
title: "URLWithString:encodingInvalidCharacters:"
framework: foundation
role: symbol
role_heading: Type Method
path: "foundation/nsurl/urlwithstring:encodinginvalidcharacters:"
---

# URLWithString:encodingInvalidCharacters:

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

## Declaration

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

## Parameters

- `URLString`: A URL location.
- `encodingInvalidCharacters`: A Boolean value that indicates whether the initializer attempts to encode any invalid characters in string.

## Discussion

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.

## See Also

### Creating a URL object

- [URLWithString:](foundation/nsurl/urlwithstring:.md)
- [init(string:)](foundation/nsurl/init(string:).md)
- [init(string:encodingInvalidCharacters:)](foundation/nsurl/init(string:encodinginvalidcharacters:).md)
- [URLWithString:relativeToURL:](foundation/nsurl/urlwithstring:relativetourl:.md)
- [init(string:relativeTo:)](foundation/nsurl/init(string:relativeto:).md)
- [fileURL(withPath:isDirectory:)](foundation/nsurl/fileurl(withpath:isdirectory:).md)
- [init(fileURLWithPath:isDirectory:)](foundation/nsurl/init(fileurlwithpath:isdirectory:).md)
- [fileURL(withPath:relativeTo:)](foundation/nsurl/fileurl(withpath:relativeto:).md)
- [init(fileURLWithPath:relativeTo:)](foundation/nsurl/init(fileurlwithpath:relativeto:).md)
- [fileURL(withPath:isDirectory:relativeTo:)](foundation/nsurl/fileurl(withpath:isdirectory:relativeto:).md)
- [init(fileURLWithPath:isDirectory:relativeTo:)](foundation/nsurl/init(fileurlwithpath:isdirectory:relativeto:).md)
- [fileURL(withPath:)](foundation/nsurl/fileurl(withpath:).md)
- [init(fileURLWithPath:)](foundation/nsurl/init(fileurlwithpath:).md)
- [fileURL(withPathComponents:)](foundation/nsurl/fileurl(withpathcomponents:).md)
- [init(resolvingAliasFileAt:options:)](foundation/nsurl/init(resolvingaliasfileat:options:).md)
