Swift iOS中的URL编码问题 [英] URL Encoding issue in swift ios

查看:347
本文介绍了Swift iOS中的URL编码问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从服务器响应中获取的网址为:

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

我正在将编码设为:

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

但是avplayer无法播放此特定url. 似乎在编码URL时遇到了一些问题

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

推荐答案

您的URL已进行百分比编码.

Your URL is already percent-encoded.

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

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)

这篇关于Swift iOS中的URL编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆