I'm currently trying to POST some JSON containing emojis to a python API. I tried feeding the NSJSONSerialization directly with the string containing the emojis from my UITextField but the serializer crashed with no meaningful explanation. Afterwards I tried to do some format conversion and ended up with something like this:
NSString *uniText = mytextField.text;
NSData *msgData = [uniText dataUsingEncoding:NSNonLossyASCIIStringEncoding];
NSString *goodMsg = [[NSString alloc] initWithData:msgData encoding:NSUTF8StringEncoding] ;
This basically works except that the resulting UTF-8 is kinda double-"escaped" resulting in the following:
"title":"\\ud83d\\udc8f\\ud83d\\udc8f\\ud83d\\udc8f\\ud83d"
Any suggestions how to fix that?
There are two difficulties:
1. Apple hosed
NSString
WRT UTF Planes 1 and above, the underlying use of UTF-16 shows through. An example is thatlength
will return 2 for one emoji character.2. Whoever decided to put emoji in Plane 1 was just being difficult, it is the first use of Plane 1 and a lot of legacy UTF code does not handle that correctly.
Example code (adapted from @Hot Licks): Updated with OP emoji
Sigh:
If myTextField.text is a valid NSString then no other conversions should be required. NSJSONSerialization will provide all necessary "escaping".