Question: Encode / Decode string to UTF-8 with Emojis

Question

Encode / Decode string to UTF-8 with Emojis

Answers 0
Added at 2017-01-02 11:01
Tags
Question

I'm developping a messaging app between Android an iOS (Swift 2.2).

Users must be able to send and receive Emojis : Hello 😀

I need to encode emojis to UTF-8 instead of Unicode.

Here my code to encode / decode from Unicode :

static func decodeUnicode(input: String) -> String {
    let ns = NSString(string: input)
    let data:NSData = ns.dataUsingEncoding(NSUTF8StringEncoding)!

    if let decoded = NSString(data:data,encoding:NSUTF8StringEncoding) as? String {
        return decoded
    }

    return ""
}

static func encodeUnicode(input: String) -> String {
    let data:NSData = input.dataUsingEncoding(NSNonLossyASCIIStringEncoding)!

    if let decoded = NSString(data:data,encoding:NSUTF8StringEncoding) as? String {
        return decoded
    }

    return ""
}

Encoding have to be compatible with Android decoding.

Answers
Source Show
◀ Wstecz