非拉丁字符Alamofire 4.0 .GET请求

问题描述:

因此,我有一个应用程序在其中引入了一个文本UITextField,然后它执行一个带有文本的Alamofire .GET请求。有时,该文本是写在Chinese, Korean, Japanese ......而Alamofire崩溃,但是,如果我在浏览器中输入中文字符的URL,它会完美返回。非拉丁字符Alamofire 4.0 .GET请求

这是网址:

https://www.googleapis.com/youtube/v3/search?part=snippet&fields=items(id,snippet(title,channelTitle,thumbnails))&order=viewCount&q=不許你注定一人&type=video&maxResults=50&key=Whatever 

正如你可以看到它包含了中国文字:

不許你注定一人 

这是Alamofire不用彷徨要求:

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 

谢谢!

你试过编码你的url后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() 
+0

工作!非常感谢! – ctabuyo