Instance Method


Returns a new string made from the receiver by replacing all characters not in the specified set with percent-encoded characters.


- (NSString *)stringByAddingPercentEncodingWithAllowedCharacters:(NSCharacterSet *)allowedCharacters;



The characters not replaced in the string. Typically, you specify one of the predefined character sets for a particular URL component, such as URLPathAllowedCharacterSet or URLQueryAllowedCharacterSet.

Return Value

Returns the encoded string, or nil if the transformation is not possible.


Entire URL strings cannot be percent-encoded, because each URL component specifies a different set of allowed characters. For example, the query component of a URL allows the “@” character, but that character must be percent-encoded in the password component.

UTF-8 encoding is used to determine the correct percent-encoded characters. Any characters in allowedCharacters outside of the 7-bit ASCII range are ignored.

See Also

Working with URL Strings


Returns a new string made from the receiver by replacing all percent encoded sequences with the matching UTF-8 characters.

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