我们如何传递字符串Mr.X&先生在URL中。
我已经尝试了这个但是这个人做了所有的角色但是&符号。
[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
答案 0 :(得分:36)
甚至更短:
@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
答案 1 :(得分:32)
-stringByAddingPercentEscapesUsingEncoding:
不执行完整的转义编码。您应该使用-replaceOccurrencesOfString:withString:
这是https://devforums.apple.com/message/15674#15674最初建议的完整列表(镜像Gamecat的列表)。正如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];
此代码肯定会对您的网址进行哈希处理。
答案 2 :(得分:4)
使用%26作为网址转义。
其他逃脱:
$ %24
& %26
+ %2B
, %2C
/ %2F
: %3A
; %3B
= %3D
? %3F
@ %40
答案 3 :(得分:0)
stringByAddingPercentEscapesUsingEncoding
也无法与+
一起正常使用。
这是一个更简单的解决方案:
[[[urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding] stringByReplacingOccurrencesOfString:@"&" withString:@"%26"] stringByReplacingOccurrencesOfString:@"+" withString:@"%2b"];
答案 4 :(得分:0)
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];
}
答案 5 :(得分:-2)
在URL中,&符号是受保护的关键字,表示包含查询字符串变量。你不能把它作为价值本身的一部分。您需要将其更改为其他内容。
以下链接指向StackOverflow上提出的相同问题:ASP.Net URLEncode Ampersand for use in Query String