7

I am developing one chat application, for that everything is working fine except. This application is in both Android and iOS platform. We want to pass emoji in the chat. In android we use UTF encoding with StringEscapeUtils. it is working perfect in android. when we pass emoji enter image description here it is encoded and stored in DB like "\u263A".

Now in android this string is also decode and shown perfect in view but some how we can not decode the same string in iOS. We have simply try to decode using UTF string. But still it is not working.

I have already follow this link Print unicode character from variable (swift)

Thanks in advance.

Community
  • 1
  • 1
Crazy Developer
  • 3,464
  • 3
  • 28
  • 62
  • where you want to print on Label or textfield? – Sagar Snehi Dec 08 '16 at 06:30
  • @SagarSnehi I want to set on both. but if solution get for textfield than also it will help me a lot. – Crazy Developer Dec 08 '16 at 06:41
  • 1
    @CrazyDeveloper probably it can help you? http://stackoverflow.com/questions/24318171/using-swift-to-unescape-unicode-characters-ie-u1234 – Nimble Dec 08 '16 at 06:52
  • 1
    if your unicode be like this than "\u{263A}" than this code will work on label labelName.text = "\u{20B9}" without {} these brackets it will not take as a string – Sagar Snehi Dec 08 '16 at 07:19

2 Answers2

7

The easiest way is to use CFStringTransform like below.

let wI = NSMutableString( string: "\\u263a" )
CFStringTransform( wI, nil, "Any-Hex/Java" as NSString, true )
someUILabel.text = wI as String
Satachito
  • 5,838
  • 36
  • 43
5

You should try this:

For Example:

let charString = "263A"
    if let charCode = UInt32(charString, radix: 16),let unicode = UnicodeScalar(charCode)
    {
        let str = String(unicode)
        print(str)
    }
    else
    {
        print("invalid input")
    }

If you want to Print on Label/TextField then:

let charString = "263A"
if let charCode = UInt32(charString, radix: 16),let unicode = UnicodeScalar(charCode)
{
    let str = String(unicode)
    CharLabel.text = str             //Print on Label
    CharTextField.text = str         //Print on TextField
}
else
{
    print("invalid input")
}
Pragnesh Vitthani
  • 2,532
  • 20
  • 28