how do we pass a string Mr.X & Mr.Y in the URL.
I have tried this but this one does all the chars but ampersand.
[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
Thanks for the help.
how do we pass a string Mr.X & Mr.Y in the URL.
I have tried this but this one does all the chars but ampersand.
[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
Thanks for the help.
In a URL, the ampersand is a protected keyword signifying the inclusion of a querystring variable. You cannot put it in as part of the value itself. You need to change it to something else.
Here is a link to the same question asked on StackOverflow: http://stackoverflow.com/questions/561954/asp-net-urlencode-ampersand-for-use-in-query-string
Use %26 as url escape.
Other escapes:
$ %24
& %26
+ %2B
, %2C
/ %2F
: %3A
; %3B
= %3D
? %3F
@ %40
-stringByAddingPercentEscapesUsingEncoding:
does not perform complete escape encoding. You should manually add the encodings using -replaceOccurrencesOfString:withString:
Here's a complete list (mirroring Gamecat's list) as originally suggested at https://devforums.apple.com/message/15674#15674 .
NSMutableString *escaped = [actionString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[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];
This code certainly makes a hash of your URL.
Or even shorter:
@implementation NSString (Escaping)
- (NSString*)stringWithPercentEscape {
return [(NSString *) CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)[[self mutableCopy] autorelease], NULL, CFSTR("=,!$&'()*+;@?\n\"<>#\t :/"),kCFStringEncodingUTF8) autorelease];
}
@end