URL编码问题在Swift ios中

我从我的服务器响应获取url为:

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

我正在做的编码为:

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

但avplayer无法播放这个特定的url。 看起来像编码URL中的一些问题

您的url已经被百分比编码。

如果再次对其进行编码,则百分比部分将被编码两次,从而导致无效的URL。

您可以通过从url中移除百分比编码并重新设置:

 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)