1:使用stringByAddingPercentEscapesUsingEncoding:
1.例子:
aPath = [aPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
2.方法
-(NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)enc;
3.描述
1.Returns a representation of the receiver using a given encoding to determine the percent escapes necessary to convert the receiver into a legal URL string.
2.It may be difficult to use this function to "clean up" unescaped or partially escaped URL strings where sequences are unpredictable.
3.See CFURLCreateStringByAddingPercentEscapes for more information.
翻译:
使用给定的编码返回一个接收者的代表,以确定将接收者转换成到合法的URL字符串所需的百分比转义。
使用此函数可能难以来清理那些序列不可预测的未转义或部分转义的URL字符串。
有关详细信息,请参阅CFURLCreateStringByAddingPercentEscapes
2:使用stringByAddingPercentEncodingWithAllowedCharacters:
1.例子:
NSString *charactersToEscape = @"?!@#$^&%*+,:;='\"`<>()[]{}/\\| ";
NSCharacterSet *allowedCharacters = [[NSCharacterSet characterSetWithCharactersInString:charactersToEscape] invertedSet];
NSString *encodedUrl = [url stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacters];
NSLog(@"\n%@\n%@",encodedUrl,encodedString1);
或:
NSString *urlStr2 = [@"http://v.juhe.cn/weather/index?format=2&cityname=北京&key=88e194ce72b455563c3bed01d5f967c5" stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
invertedSet 作用:
A character set containing only characters that don’t exist in the receiver.
Using the inverse of an immutable character set is much more efficient than inverting a mutable character set.
中文:
仅包含接收器中不存在的字符的字符集。
反转不可变字符集比反转可变字符集更有效率。
URLQueryAllowedCharacterSet 作用:
Returns the character set for characters allowed in a query URL component.
The query component of a URL is the component immediately following a question mark (?).
For example, in the URL http://www.example.com/index.php?key1=value1#jumpLink, the query component is key1=value1.
中文:
返回一个查询URL组件中允许字符的字符集。
URL的查询组件是一个紧跟着问号(?)的组件。
例如,在URL http://www.example.com/index.php?key1=value1#jumpLink中,查询组件是key1 = value1。
2.方法:
- (NSString *)stringByAddingPercentEncodingWithAllowedCharacters:
(NSCharacterSet *)allowedCharacters
3.描述:
1.Returns a new string made from the receiver by replacing all characters not in the allowedCharacters set with percent encoded characters.
2.UTF-8 encoding is used to determine the correct percent encoded characters.
3.Entire URL strings cannot be percent-encoded.
4.This method is intended to percent-encode an URL component or subcomponent string, NOT the entire URL string.
5.Any characters in allowedCharacters outside of the 7-bit ASCII range are ignored.
翻译:
返回一个由接收者制成的新字符串,使用百分比编码字符替换所有不在allowenCharacters集合中的字符
UTF-8编码用于确定正确的百分比编码字符。
整个URL字符串不能进行百分比编码。
此方法用于URL组件或子组件字符串的百分比编码,而不是整个URL字符串。
任何在allowedCharacters中的字符,在7-bit ASCII 范围之外的,都将被忽略。
3.使用CFURLCreateStringByAddingPercentEscapes
NSString *url = @"ertehtt""p://xxdsdscrg?!@#$^&%*+,:;='\"`<>()[]{}/\\| ";
CFStringRef encodedCFString = CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(__bridge CFStringRef) url,
nil,
CFSTR("?!@#$^&%*+,:;='\"`<>()[]{}/\\| "),
kCFStringEncodingUTF8);
NSString *encodedString1 = [[NSString alloc] initWithString:(__bridge_transfer NSString*) encodedCFString];