I'm having trouble googling this.
Does Objective-C have an equivalent method for encoding URI Component?
.asp
This seems to be a very mon use case but I can't find any documentation about it in Objective-C.
Thanks.
I'm having trouble googling this.
Does Objective-C have an equivalent method for encoding URI Component?
http://www.w3schools./jsref/jsref_encodeuriponent.asp
This seems to be a very mon use case but I can't find any documentation about it in Objective-C.
Thanks.
Share Improve this question asked Jul 2, 2012 at 22:46 TrungTrung 1551 silver badge5 bronze badges4 Answers
Reset to default 6-[NSString stringByAddingPercentEscapesUsingEncoding:]
is the simplest way to do this.
off course, you can choose this methods, I hope that will be helpful.
//encode URL string
+(NSString *)URLEncodedString:(NSString *)str
{
NSString *encodedString = (NSString *)
CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)str,
NULL,
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8));
return encodedString;
}
}
//decode URL string
+(NSString *)URLDecodedString:(NSString *)str
{
NSString *decodedString=(__bridge_transfer NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL, (__bridge CFStringRef)str, CFSTR(""), CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
return decodedString;
}
Now stringByAddingPercentEscapesUsingEncoding:
is deprecated. Here's the iOS 7+ equivalent of encodeURIComponent
:
[str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]
Using CFURLCreateStringByAddingPercentEscapes
or stringByAddingPercentEscapesUsingEncoding
is not ideal since those are deprecated methods.
Dave's answer is on the right track but is not explicit enough.
We want to call stringByAddingPercentEncodingWithAllowedCharacters
but we need to choose a character set. Despite what our intuiton might say, NSCharacterSet.URLQueryAllowedCharacterSet
is actually not a good choice. It refers to the whole query part of the URL, not just to values of query parameters so it allows characters like ?
or &
as well, which are not valid here. What you want is to manually build the allowed characters set according to the documentation and use that. The allowed chars for encodeURIComponent()
are:
A–Z a–z 0–9 - _ . ! ~ * ' ( )
That gives us this solution:
// Note: Using NSCharacterSet.URLQueryAllowedCharacterSet is inappropriate for this purpose.
// It contains the allowed chars for the whole Query part of the URL (including '?' or '&'),
// not just for the values of query parameters (where chars are more restricted).
+ (NSString *)encodeURIComponent:(NSString *)uriComponent {
// https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
NSString *allowedChars = @"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789"
"-_.!~*'()";
NSCharacterSet *charSet = [NSCharacterSet characterSetWithCharactersInString:allowedChars];
return [uriComponent stringByAddingPercentEncodingWithAllowedCharacters:charSet];
}
+ (NSString *)decodeURIComponent:(NSString *)uriComponent {
return [uriComponent stringByRemovingPercentEncoding];
}