Iphone SDk:在URLstring中带有与符号的问题

我们如何在URL中传递stringMr.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:手动添加编码-replaceOccurrencesOfString:withString:

这是一个完整的列表(镜像Gamecat的列表)最初build议在https://devforums.apple.com/message/15674#15674 。 正如尼克指出的,这是昂贵的,所以不要简单地包括完整的列表,而不考虑您的实际要求转义。

 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]; 

这段代码当然会对你的URL进行散列。

使用%26作为url转义。

其他逃脱:

 $ %24 & %26 + %2B , %2C / %2F : %3A ; %3B = %3D ? %3F @ %40 

stringByAddingPercentEscapesUsingEncoding也无法正确使用+

这是一个更简单的解决scheme:

 [[[urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding] stringByReplacingOccurrencesOfString:@"&" withString:@"%26"] stringByReplacingOccurrencesOfString:@"+" withString:@"%2b"]; 

在URL中,和号是一个受保护的关键字,表示包含查询stringvariables。 你不能把它作为价值本身的一部分。 你需要把它改成别的东西。

这里是一个链接到相同的问题StackOverflow询问: ASP.Net URLEncode&符号查询string中使用