Skip to content

Instantly share code, notes, and snippets.

@theiostream
Last active December 11, 2015 17:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theiostream/4635923 to your computer and use it in GitHub Desktop.
Save theiostream/4635923 to your computer and use it in GitHub Desktop.
URL-Encode NSStrings and NSDictionaries.
// From Cocoanetics (s/&amp/&)
static NSString *NSStringURLEncode(NSString *string) {
return [(NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)string, NULL, CFSTR("!*'();:@&;=+$,/?%#[]"), kCFStringEncodingUTF8) autorelease];
}
static NSString *NSDictionaryURLEncode(NSDictionary *dict) {
NSMutableString *ret = [NSMutableString string];
NSArray *allKeys = [dict allKeys];
for (NSString *key in allKeys) {
[ret appendString:NSStringURLEncode(key)];
[ret appendString:@"="];
[ret appendString:NSStringURLEncode([dict objectForKey:key])];
[ret appendString:@"&"];
}
return [ret substringToIndex:[ret length]-1];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment