Emoji doesn't show up in NSAttributedString when typing from iOS keyboard when typing on Android

I am making a messaging app, and when I send emoji from the Android side, it shows great on the iOS side, but the iOS side cannot (seem to) display emojis from the iOS native keyboard!

The label on which I display emoji uses attribute text, and the text is from HTML. The following code is used to create an NSAttributedString

var msg = getTextForDisplay()//Essentially gets the plain text msg = "<span style=\"font-family: Helvetica; font-size:14pt;\">" + msg + "</span>" if let data = msg.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false){ let attributed = try? NSAttributedString(data: data, options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType ], documentAttributes: nil) return attributed } 

Even a stranger, when I send emoji from iOS, although it does not appear on the shortcut as described above, it shows perfectly on the Android side. On the iOS side, the shortcut displays what seems funny (unicode chasracters, maybe?)

eg: enter image description here

I am absolutely sure that the error is related to the display of emoji, because when you print to the console, I see real emoji, and also when saving to the device storage and later read on Mac, it is real emoji. But when emoji loads in attribute tag, it shows jibberish

Any help is appreciated. I understand that the problem is with the encoding. I'm just not sure what the problem is and how to fix it.

+5
source share
1 answer

In the comments, we confirm that the original string is in order. There is only one option to change! For strings, Swift typically uses UTF-16 . You lose information by processing it like UTF-8.

Edit:

msg.dataUsingEncoding (NSUTF8StringEncoding

To:

msg.dataUsingEncoding (NSUTF 16 StringEncoding

+2
source

Source: https://habr.com/ru/post/1233092/


All Articles