0

i want to use text input and emoji from iOS default keyboard and send it to server and show that text to label but i am not able to display emojis.it only display text but not emojis. if i do it locally than it will display emoji.

  self.labelName.text = TextFiled.text

output : "test "

but when i send send it to server and receive from it from api than emoji is gone. output : "test"

please dont down vote my question without any reason

Prashant Ghimire
  • 518
  • 4
  • 20
  • Refer- http://stackoverflow.com/questions/11382753/change-the-keyboard-layout-to-emoji . You can create custom keyboard and display emoji's – Rince Thomas Oct 25 '16 at 05:04
  • can you show me how you using emoji in your code ?? and show me what you have done so far so will guide as per your code efforts. – CodeChanger Oct 25 '16 at 05:07
  • i want to use both alphabet and emoji @Signare – Prashant Ghimire Oct 25 '16 at 05:08
  • i have done self.labelName.text = TextFiled.text only so far @CodeChanger – Prashant Ghimire Oct 25 '16 at 05:09
  • so is it showing emoji in your label ?? or that is your problem that can not see emoji in your label ? – CodeChanger Oct 25 '16 at 05:10
  • my problem is when i send emoji and text to server using api. it only shows text @CodeChanger – Prashant Ghimire Oct 25 '16 at 05:12
  • i just want to do like whats app. it support default keyboard @Signare – Prashant Ghimire Oct 25 '16 at 05:12
  • Get the Unicode value of the emoji and send to server. http://apps.timwhitlock.info/emoji/tables/unicode – Rince Thomas Oct 25 '16 at 05:13
  • @PrashantGhimire try my answer its solve your problem if any problem you face in my code then tell me – Himanshu Moradiya Oct 25 '16 at 05:17
  • Once I have experienced this kind of issue. In my case the emoji is coming as some string value along with some forward slash, That was the server side issue, in our case the server is ruby, while they save ruby appends some extra slash to unicode and that was the reason of issue. once they resolved it came correctly. Just check with server guy what is coming and what is happening to your emoji. Yes first check the response is it only text or some unicode, then accordingly check with server guy. hope it will help – Janmenjaya Oct 25 '16 at 05:37
  • i have talk to the server guy . he said it accept unicode too. i dont know what to say please explain in more detail.@Janmenjaya – Prashant Ghimire Oct 25 '16 at 05:40
  • get the unicode of the corresponding emoji and send to server. – Rince Thomas Oct 25 '16 at 06:03
  • thank you for the suggestion @Signare actually answer is correct by Himanshu Moradiya – Prashant Ghimire Oct 25 '16 at 06:40

2 Answers2

2

Swift 3.0 Extension solution:

You need to encode and decode emoji's on iOS side when sending it to server. You can do it as below.

extension String {

    func encodeChatString() -> String? {
        if let encodedTextData = self.data(using: .nonLossyASCII) {
            return String(data: encodedTextData, encoding: .utf8)
        }

        return nil
    }

    func decodeChatString() -> String? {
        let trimmedString = self.trimmingCharacters(in: .whitespacesAndNewlines)
        if let stringData = trimmedString.data(using: .utf8) {
            let messageString = String(data: stringData, encoding: .nonLossyASCII)
            return messageString
        }

        return nil
    }
}

When you send message encode string like below:

message.encodeChatString()

When you receive message decode string like below:

message.decodeChatString()
Parth Adroja
  • 13,198
  • 5
  • 37
  • 71
1

When send a data to server use this method .

let data1 = txtMessage.text.dataUsingEncoding(NSNonLossyASCIIStringEncoding)!
let finalmessage = String(data: data1, encoding: NSUTF8StringEncoding)

when you get a response from server before set in label use this method.

 let trimmedString = YOURSERVER_RESPONSE_STRING.stringByTrimmingCharactersInSet(
                NSCharacterSet.whitespaceAndNewlineCharacterSet())
 let data2 = trimmedString.dataUsingEncoding(NSUTF8StringEncoding)!
 let messagestring = String(data: data2, encoding: NSNonLossyASCIIStringEncoding)
 YOURLABEL.text = messagestring as String

Try this your problem solve.

Himanshu Moradiya
  • 4,769
  • 4
  • 25
  • 49