Skip to content

Instantly share code, notes, and snippets.

@leovandriel
Created September 26, 2012 10:34
Show Gist options
  • Save leovandriel/3787259 to your computer and use it in GitHub Desktop.
Save leovandriel/3787259 to your computer and use it in GitHub Desktop.
Objective-C methods for percent-escaping NSString
// Objective-C methods for percent-escaping NSString, for use in NSURL.
// This approach does full escaping, instead of the 'smart' escaping that
// is provided by [NSString stringByReplacingPercentEscapesUsingEncoding:].
// License: Public Domain
// Author: Leonard van Driel, 2012
@implementation NSString (EscapeForURL)
- (NSString *)escapeForURL
{
return (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL, (__bridge CFStringRef)self, NULL, CFSTR("*'();:@&=+$,/?!%#[]"), kCFStringEncodingUTF8));
}
- (NSString *)unescapeForURL
{
return (NSString *)CFBridgingRelease(CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL, (__bridge CFStringRef)self, CFSTR(""), kCFStringEncodingUTF8));
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment