URL-encoding and HTML-encoding NSStrings

Mustafa picture Mustafa · Nov 3, 2009 · Viewed 23.4k times · Source

Is their a method to encode/decode HTML and URL (in Xcode, using Objective-C)?

[NSString stringWithContentsOfFile:<#(NSString *)path#> encoding:<#(NSStringEncoding)enc#> error:<#(NSError **)error#>]

This doesn't seem to work how i expected. I thought it will convert special characters like "<" to equivalent HTML entities i.e. "<" in this case.

Here's a reference to the w3school link related to this topic (general):

HTML URL Encoding Reference

HTML Entities Reference

Thanking in anticipation.

Answer

zaph picture zaph · Nov 3, 2009

Returns a representation of the receiver using a given encoding to determine the percent escapes necessary to convert the receiver into a legal URL string.

- (NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)encoding

and

Returns a new string made by replacing in the receiver all percent escapes with the matching characters as determined by a given encoding.

- (NSString *)stringByReplacingPercentEscapesUsingEncoding:(NSStringEncoding)encoding