0

I currently receive emojis in a payload in the following format:

\\U0001F6A3\\U0000200D\\U00002640\\U0000FE0F

which represents "‍♀️"

However, if I try to display this, it only shows the string above (escaped with 1 less ), not the emoji e.g.

NSString *emoji = payload[@"emoji"];
NSLog(@"%@", emoji) then displays as \U0001F6A3\U0000200D\U00002640\U0000FE0F

It's as if the unicode escape it not being recognised. How can I get the string above to show as an emoji?

Please assume that the format the data is received in from the server cannot be changed.

UPDATE

I found another way to do it, but I think the answer by Albert posted below is better. I am only posting this for completeness and reference:

NSArray *emojiArray = [unicodeString componentsSeparatedByString:@"\\U"];
NSString *transformedString = @"";

for (NSInteger i = 0; i < [emojiArray count]; i++) {

    NSString *code = emojiArray[i];
    if ([code length] == 0) continue;
    NSScanner *hexScan = [NSScanner scannerWithString:code];
    unsigned int hexNum;
    [hexScan scanHexInt:&hexNum];
    UTF32Char inputChar = hexNum;
    NSString *res = [[NSString alloc] initWithBytes:&inputChar length:4 encoding:NSUTF32LittleEndianStringEncoding];
    transformedString = [transformedString stringByAppendingString:res];
}
RunLoop
  • 20,288
  • 21
  • 96
  • 151

2 Answers2

3

Remove the excess backslash then convert with a reverse string transform stringByApplyingTransform. The transform must use key "Any-Hex" for emojis.

NSString *payloadString = @"\\U0001F6A3\\U0000200D\\U00002640\\U0000FE0F";
NSString *unescapedPayloadString = [payloadString stringByReplacingOccurrencesOfString:@"\\\\" withString:@"\\"];
NSString *transformedString = [unescapedPayloadString stringByApplyingTransform:@"Any-Hex" reverse:YES];

NSLog(@"%@", transformedString);//logs "‍♀️"
Albert Renshaw
  • 17,282
  • 18
  • 107
  • 195
  • Showing that this is a duplicate of https://stackoverflow.com/questions/57103365/objective-c-how-to-decode-this-utf16-string etc. :) – matt Mar 16 '20 at 21:29
  • 1
    @matt What a small world, I read your initial comment on this post that it would have to be parsed manually, then I sleuthed for about 50 minutes to finally find that solution, upvoted you there, and just now realize you yourself wrote that solution many months ago haha. I had been working w/ transforms using "Any-Hex/Unicode" to no avail but using just "Any-Hex" was the key – Albert Renshaw Mar 16 '20 at 21:54
  • Actually there are only about six users on Stack Overflow so this was bound to happen. :) – matt Mar 16 '20 at 21:58
  • 1
    Thank you @AlbertRenshaw. I also found an alternate solution I posted in my question, though I think yours is better. I tested both on a variety of emojis, especially those with many surrogate pairs, and all works well. – RunLoop Mar 17 '20 at 04:05
  • Damn u beat me to it. I was just about to post this solution. – GeneCode Mar 18 '20 at 05:53
1

I investigated this, and it seems you may not be receiving what you say you are receiving. If you see \U0001F6A3\U0000200D\U00002640\U0000FE0F in your NSLog, chances are you are actually receiving \\U0001F6A3\\U0000200D\\U00002640\\U0000FE0F at your end instead. I tried using a variable

NSString *toDecode = @"\U0001F6A3\U0000200D\U00002640\U0000FE0F";
self.tv.text = toDecode;

And in textview it is displaying the emoji fine. enter image description here

So you got to fix that first and then it will display well.

GeneCode
  • 7,545
  • 8
  • 50
  • 85
  • Thanks, I am indeed receiving double \\ as you pointed out, but I can't seem to replace \\ with \. – RunLoop Mar 16 '20 at 09:19