URL Encoding issue in swift ios

为君一笑 提交于 2019-12-25 09:45:50

问题


I am getting url from my server response as:

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

and i am doing encoding as :

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

but avplayer not able to play this particular url . seems like some issue in encoding URL


回答1:


Your URL is already percent-encoded.

If you encode it again, the percent parts will be encoded twice, giving an invalid URL.

You can see that by removing the percent encoding from your URL and setting it again:

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)


来源:https://stackoverflow.com/questions/38345687/url-encoding-issue-in-swift-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!