Randomize Everything

Icon

And create the perfect world

Encoding HTML entities In Objective C

When sending data to a backend server you sometimes need to encode special characters to HTML entities. To encode those characters in a string use the method: stringByAddingPercentEscapesUsingEncoding. Consider the following example to make it a bit more clear:

NSString *urlString = [NSString stringWithFormat:@"%@%@", @"http://www.this-is-a-url.com/accept.php?identifier=", uniqueDeviceIdentifier];
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"Upload to server: %@", urlString);

Short note: the first line of the example shows you how to concatenate two strings.

Category: Objective-C, iPad, iPhone

Tagged: , , , , , , ,

Leave a Reply