swift ios中的URL编码问题

时间:2016-07-13 07:52:55

标签: ios swift avplayer url-encoding

我从服务器响应中获取url:

https://baseURL/The+Man+in+the+High+Castle+Official+Trailer+%E2%80%93+2+th.m3u8

我正在编码:

 videoURL = videoURL.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!

但是avplayer无法播放此特定网址。 似乎在编码URL

中存在一些问题

1 个答案:

答案 0 :(得分:3)

您的网址已经过百分比编码。

如果再次对其进行编码,百分比将被编码两次,从而产生无效的网址。

您可以通过从网址中删除百分比编码并再次进行设置来查看:

let base = "https://baseURL/The+Man+in+the+High+Castle+Official+Trailer+%E2%80%93+2+th.m3u8"
let decoded = base.stringByRemovingPercentEncoding!
print(decoded)
let encoded = decoded.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLFragmentAllowedCharacterSet())!
print(encoded)