sulabh sulabh - 4 months ago 32
Swift Question

URL Encoding issue in swift ios

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

Answer

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)
Comments