简体   繁体   English

使用NSCharcterSet进行URL编码

[英]URL Encoding with NSCharcterSet

I have few video files and image files stored in applications directory.I was using stringByAddingPercentEscapesUsingEncoding: to convert into a legal URL string. 我在应用程序目录中存储的视频文件和图像文件很少。我使用stringByAddingPercentEscapesUsingEncoding:转换为合法的URL字符串。 Since it is deprecated and we are to use stringByAddingPercentEncodingWithAllowedCharacters: instead, I am not able to get the correct URL for loading the pdf or playing the video. 由于不建议使用,我们将使用stringByAddingPercentEncodingWithAllowedCharacters:相反,我无法获得用于加载pdf或播放视频的正确URL。 I tried all NSCharacterSet available. 我尝试了所有可用的NSCharacterSet。 It is adding extra %252020 for a space and which is creating a problem. 它为空间增加了额外的%252020,这带来了问题。

Using stringByAddingPercentEscapesUsingEncoding 使用stringByAddingPercentEscapesUsingEncoding

file:///var/mobile/Containers/Data/Application/1392B0FE-F998-45A4-A398-A3ACB47ECE26/Library/Private%20Documents/Sample%20Image%20To%20Be%20Loaded.jpg 文件:///var/mobile/Containers/Data/Application/1392B0FE-F998-45A4-A398-A3ACB47ECE26/Library/Private%20Documents/Sample%20Image%20To%20Be%20Loaded.jpg

Using ** stringByAddingPercentEncodingWithAllowedCharacters** 使用** stringByAddingPercentEncodingWithAllowedCharacters **

file:///var/mobile/Containers/Data/Application/1392B0FE-F998-45A4-A398-A3ACB47ECE26/Library/Private%2520Documents/Sample%2520Image%2520To%2520Be%2520Loaded.jpg 文件:///var/mobile/Containers/Data/Application/1392B0FE-F998-45A4-A398-A3ACB47ECE26/Library/Private%2520Documents/Sample%2520Image%2520To%2520Be%2520Loaded.jpg

And this couldn't find the file and hence couldn't load it. 而且这找不到文件,因此无法加载它。 Any help is much appreciated. 任何帮助深表感谢。

The way you get %2520 is when your url already has a %20 in it, and gets url-encoded again, which transforms the %20 to %2520. 获取%2520的方式是当您的网址中已经包含%20,然后再次对其进行网址编码,从而将%20转换为%2520。 %255B is what you get when you encode square bracket twice. 当对方括号进行两次编码时,将得到%255B。

you can try the following code in en empty project: 您可以在空项目中尝试以下代码:

NSString * str = @"/var/mobile/Containers/Data/Application/1392B0FE-F998-45A4-A398-A3ACB47ECE26/Library/Private Documents/Sample Image To Be Loaded.jpg";
NSString * encodedString = [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
NSLog(@"encodedString = %@", encodedString);
NSString * encodedStringAgain = [encodedString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
NSLog(@"encodedStringAgain = %@", encodedStringAgain);

It prints: 它打印:

encodedString = /var/mobile/Containers/Data/Application/1392B0FE-F998-45A4-A398-A3ACB47ECE26/Library/Private%20Documents/Sample%20Image%20To%20Be%20Loaded.jpg encodeString = / var / mobile /容器/数据/应用程序/1392B0FE-F998-45A4-A398-A3ACB47ECE26/Library/Private%20Documents/Sample%20Image%20To%20Be%20Loaded.jpg

encodedStringAgain = /var/mobile/Containers/Data/Application/1392B0FE-F998-45A4-A398-A3ACB47ECE26/Library/Private%2520Documents/Sample%2520Image%2520To%2520Be%2520Loaded.jpg encodeStringAgain = / var / mobile /容器/数据/应用程序/1392B0FE-F998-45A4-A398-A3ACB47ECE26/Library/Private%2520Documents/Sample%2520Image%2520To%2520Be%2520Loaded.jpg

All you have to do in your code is : 您在代码中要做的就是:

NSURL *urlToLoad = [NSURL fileURLWithPath: fullPath];

With fullPath being the original string without any encoding. fullPath是没有任何编码的原始字符串。

您可以使用

NSString * encodedString = [@"stringURl to encode" stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM