Swift-特殊字符的编码和解码字符串 [英] Swift - Encoding and Decoding String for special characters

查看:976
本文介绍了Swift-特殊字符的编码和解码字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题:我目前正在开发一个包含即时聊天的应用程序(使用FCM). 除非我尝试发送特殊字符(例如é"或表情符号),否则一切都正常. 收到的推送通知包含一个好的字符串,但是当讨论保存在服务器上然后重新加载时,我无法阅读它们并获得UTF8文本,如ê"的"u00ea". 我对编码等并不了解很多,但是如果你们能帮助我,我会很高兴的!

Here is my problem : I'm currently developping an app including an instant chat (using FCM). Every thing works except when i try to send special characters like "é" or emojis. The push notification received contains the good string but when the discussion is saved on the server and then reloaded, I can't read them and get UTF8 text like "u00ea" for "ê". I don't really know a lot about encoding etc. but if you guys could help I would be very happy !

这是我用来发送消息的代码:

Here is the code i use to send a message :

    func sendMessage(_ completion: @escaping (_ response: String) -> ()) {

    DispatchQueue.global(qos: .userInitiated).async { () -> Void in
        let urlString = //Server adress
        let url = URL(string: urlString)

        var request = URLRequest(url: url!)
        let set = CharacterSet()
        request.httpMethod = "POST"
        let postString = "Message=" + /*String text to send*/
        request.httpBody = postString.data(using: String.Encoding.utf8)

    let task = URLSession.shared.dataTask(with: request, completionHandler: { (data, response, error) in

        })
        task.resume()
    }
}

非常感谢!

PS:我是问堆栈溢出的新手,所以请随时询问更多细节

PS : I'm new to asking to stackoverflow so don't hesitate to ask for more details

根据Rob的要求进行 我发送了"testéè"消息.
另一部电话通过推送通知接收到"testéè".
当一部电话从服务器中加载服务器中的讨论时,其答案就存储在服务器中.

Edit as asked by Rob : I sent the message "test é è".
The other phone received "test é è" via push notification.
When one phone loads discussion from the server where its stored the answer from the server is.

    [{"content":"test u00e9 u00e8","sender":"103","timestamp":"1475491191"}]

我也不对服务器进行编码,但是他也在开发一个可以很好地处理特殊字符的android应用.

Also I'm not coding the server, but the guy who is is also making an android app which works really fine with special characters.

编辑#2 :我也尝试使用此代码

Edit #2 : I also try this code

    var request = URLRequest(url: url!)
        let set = CharacterSet()
        request.httpMethod = "POST"
        let postString = "Message=" + self.message.addingPercentEncoding(withAllowedCharacters: set)!
        let bytesArray = UTF8Encoding.decode(postString)

        request.httpBody = Data(bytes: UnsafePointer<UInt8>(bytesArray), count: bytesArray.count)

没什么区别.

非常感谢您的帮助.

推荐答案

好的,我想我知道了,

如建议的那样,部分原因是服务器的实现.由于其安全性,它过滤了某些字符,这就是为什么它不起作用的原因.

As suggested, it was due partially to the server implementation. Due to its security it filtered certain characters and that's why it wasn't working.

我们已纠正了该问题,但是当讨论是从iOS到Android或从Android到iOS时,它仍然无法正常工作.

We have corrected the problem but it's still doesn't work when the discussion is from iOS to Android or Android to iOS.

非常感谢您的帮助!

这是我的最终代码:

var request = URLRequest(url: url!)
request.httpMethod = "POST"
let postString = "Message=" + (self.message.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed))!
request.httpBody = postString.data(using: String.Encoding.utf8, allowLossyConversion: true)

这篇关于Swift-特殊字符的编码和解码字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆