Reputation: 155
I'm having trouble googling this.
Does Objective-C have an equivalent method for encoding URI Component?
http://www.w3schools.com/jsref/jsref_encodeuricomponent.asp
This seems to be a very common use case but I can't find any documentation about it in Objective-C.
Thanks.
Upvotes: 4
Views: 4077
Reputation: 194
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.org/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];
}
Upvotes: 0
Reputation: 13629
Now stringByAddingPercentEscapesUsingEncoding:
is deprecated. Here's the iOS 7+ equivalent of encodeURIComponent
:
[str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]
Upvotes: 0
Reputation: 21
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;
}
Upvotes: 1
Reputation: 243156
-[NSString stringByAddingPercentEscapesUsingEncoding:]
is the simplest way to do this.
Upvotes: 6