Skip to content

Instantly share code, notes, and snippets.

@cbarrett
Created January 19, 2012 20:31
Show Gist options
  • Save cbarrett/1642401 to your computer and use it in GitHub Desktop.
Save cbarrett/1642401 to your computer and use it in GitHub Desktop.
Had this laying around for quite a while -- really useful and kind of dumb it's not built in...
// stringByAddingPercentEscapesUsingEncoding is pretty conservative about escaping characters, which is fine most of the time. This method however escapes *all special characters*, allowing any arbitrary string to be used when building any part of a URL.
// (c) 2011 Springs & Struts. Released under MIT-style license. See: http://www.opensource.org/licenses/mit-license.php
@interface NSString (SSURLEscaping)
- (NSString *)ss_stringByEscapingAllURLCharactersForRealWithEncoding:(NSStringEncoding)encoding;
@end
@implementation NSString (SSURLEscaping)
- (NSString *)ss_stringByEscapingAllURLCharactersForRealWithEncoding:(NSStringEncoding)encoding
{
return [(id)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)self, NULL, CFSTR(";/?:@&=+$,"), CFStringConvertNSStringEncodingToEncoding(encoding)) autorelease];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment