Last active
December 11, 2015 17:38
-
-
Save theiostream/4635923 to your computer and use it in GitHub Desktop.
URL-Encode NSStrings and NSDictionaries.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// From Cocoanetics (s/&/&) | |
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