如何在URL中传递字符串“Mr.X&Mr.Y”。
我尝试过这个方法,但它只转换了所有字符,而不是“&”。
[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
如何在URL中传递字符串“Mr.X&Mr.Y”。
我尝试过这个方法,但它只转换了所有字符,而不是“&”。
[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
@implementation NSString (Escaping)
- (NSString*)stringWithPercentEscape {
return [(NSString *) CFURLCreateStringByAddingPercentEscapes(
NULL,
(CFStringRef)[[self mutableCopy] autorelease],
NULL,
CFSTR("=,!$&'()*+;@?\n\"<>#\t :/"),
kCFStringEncodingUTF8) autorelease];
}
@end
这里是一个作为ARC符合性内联函数帮助程序的示例:
#if __has_feature(objc_arc)
static inline NSString *hxURLEscape(NSString *v) {
static CFStringRef _hxURLEscapeChars = CFSTR("=,!$&'()*+;@?\r\n\"<>#\t :/");
return ((__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes(
NULL,
(__bridge CFStringRef)[v mutableCopy],
NULL,
_hxURLEscapeChars,
kCFStringEncodingUTF8));
}
#endif
-stringByAddingPercentEscapesUsingEncoding:
方法并不能对所有字符进行完整的编码,你应该手动使用-replaceOccurrencesOfString:withString:
方法添加编码。
以下是一个完整的列表(与Gamecat给出的列表相同),最初建议在https://devforums.apple.com/message/15674#15674上。正如Nick所指出的那样,这个方法很费资源,因此在考虑转义的实际需求时不要简单地包含完整的列表。
NSMutableString *escaped = [actionString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[escaped replaceOccurrencesOfString:@"$" withString:@"%24" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"&" withString:@"%26" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"+" withString:@"%2B" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"," withString:@"%2C" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"/" withString:@"%2F" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@":" withString:@"%3A" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@";" withString:@"%3B" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"=" withString:@"%3D" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"?" withString:@"%3F" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"@" withString:@"%40" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@" " withString:@"%20" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"\t" withString:@"%09" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"#" withString:@"%23" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"<" withString:@"%3C" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@">" withString:@"%3E" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"\"" withString:@"%22" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"\n" withString:@"%0A" options:NSCaseInsensitiveSearch range:wholeString];
使用%26作为URL转义。
其他转义:
$ %24
& %26
+ %2B
, %2C
/ %2F
: %3A
; %3B
= %3D
? %3F
@ %40
CFURLCreateStringByAddingPercentEscapes
已经被弃用(自iOS9起),那么我们可以将URLQueryAllowedCharacterSet修改一下,去掉保留字符,从而允许它们被相对高效地百分比编码。
- (NSString *)URLQueryValueEncodedString:(NSString *)string {
static NSMutableCharacterSet *_allowedCharacterSet = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSString *reservedCharacters = @"=,!$&'()*+;@?\r\n\"<>#\t :/[]%";
_allowedCharacterSet = [NSCharacterSet URLQueryAllowedCharacterSet].mutableCopy;
[_allowedCharacterSet removeCharactersInString:reservedCharacters];
});
return [string stringByAddingPercentEncodingWithAllowedCharacters:_allowedCharacterSet];
}
stringByAddingPercentEscapesUsingEncoding
也无法正确处理 +
。
这里有一个更简单的解决方案:
[[[urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding] stringByReplacingOccurrencesOfString:@"&" withString:@"%26"] stringByReplacingOccurrencesOfString:@"+" withString:@"%2b"];
self
替换[[self mutableCopy] autorelease]
。有关此问题的更多详细信息,请参阅CFURLCreateStringByAddingPercentEscapes
的文档 http://developer.apple.com/library/mac/#documentation/CoreFoundation/Reference/CFURLRef/Reference/reference.html - Holtwick