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]
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]
Or even shorter:
@implementation NSString (Escaping)
- (NSString*)stringWithPercentEscape {
return [(NSString *) CFURLCreateStringByAddingPercentEscapes(
NULL,
(CFStringRef)[[self mutableCopy] autorelease],
NULL,
CFSTR("=,!$&'()*+;@?\n\"<>#\t :/"),
kCFStringEncodingUTF8) autorelease];
}
@end
And here it is again as an ARC conform inline function helper:
#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
[[self mutableCopy] autorelease]
by self
. More details about it are available in the documentation of CFURLCreateStringByAddingPercentEscapes
developer.apple.com/library/mac/#documentation/CoreFoundation/… –
Plato -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. As Nick points out, this is expensive so do not simply include the full list without considering your real requirements for escaping.
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];
This code certainly makes a hash of your URL.
Use %26 as url escape.
Other escapes:
$ %24
& %26
+ %2B
, %2C
/ %2F
: %3A
; %3B
= %3D
? %3F
@ %40
stringByAddingPercentEscapesUsingEncoding
also doesn't work properly with +
.
Here is a simpler solution:
[[[urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding] stringByReplacingOccurrencesOfString:@"&" withString:@"%26"] stringByReplacingOccurrencesOfString:@"+" withString:@"%2b"];
CFURLCreateStringByAddingPercentEscapes
is deprecated (since iOS9), so how about just modifying the URLQueryAllowedCharacterSet to remove the reserved characters, thereby allowing them to be percent encoded relatively efficiently?
- (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];
}
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: ASP.Net URLEncode Ampersand for use in Query String
© 2022 - 2024 — McMap. All rights reserved.