Following method will encode and decode NSString. Characters to leave unescaped (NULL = all escaped sequences are replaced).
- (NSString *)URLEncodedString {
CFStringRef url = CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)self, NULL, CFSTR("!*'();:@&=+$,/?%#[]"), kCFStringEncodingUTF8); // for some reason, releasing this is disasterous
NSString *result = (NSString *)url;
[result autorelease];
return result;
}
- (NSString *)URLDecodedString {
CFStringRef url = CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault, (CFStringRef)self, CFSTR(""), kCFStringEncodingUTF8);
NSString *result = (NSString *)url;
[result autorelease];
return result;
}
0 Comment(s)