ctabuyo ctabuyo - 1 year ago 130
Swift Question

Non-latin characters Alamofire 4.0 .GET Request

So, I have an App in which you introduce a text in a

and then it performs an Alamofire
request with text. Sometimes, that text is written in
Chinese, Korean, Japanese
... And Alamofire crashes, however if I type the URL in the browser with the Chinese characters it returns perfectly.

This is the URL:


As you can see it contains the Chinese text:


And this is the Alamofire .GET request:

let url = "https://www.googleapis.com/youtube/v3/search?part=snippet&fields=items(id,snippet(title,channelTitle,thumbnails))&order=viewCount&q=\(Text)&type=video&maxResults=50&key=Whatever"
let nUrl = url.replacingOccurrences(of: " ", with: "+")
Alamofire.request(nUrl, method: .get).validate().responseJSON { response in


Answer Source

Have you try to encode your url following RFC 3986 ?

extension String {
    func stringByAddingPercentEncodingForRFC3986() -> String? {
        let unreserved = "-._~/?"
        let allowed = NSMutableCharacterSet.alphanumeric()
        allowed.addCharacters(in: unreserved)
        return self.addingPercentEncoding(withAllowedCharacters: allowed as CharacterSet)


let nUrl = url.stringByAddingPercentEncodingForRFC3986()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download