Proper URL path escaping in Cocoa
Don't do this:
NSString *escaped = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Do this:
CFStringRef escaped = CFURLCreateStringByAddingPercentEscapes(NULL,
(CFStringRef)path,
NULL,
CFSTR(";?#"),
kCFStringEncodingUTF8);
Written by Adam Yanalunas
Related protips
2 Responses
Why?
over 1 year ago
·
Because stringByAddingPercentEscapesUsingEncoding
alone does not strictly conform to what can be escaped in arguments and you may end up with skipped, unencoded characters in your string. That would result in a malformed URL with incorrect parameters.
That would result in great sadness. And probably sadness as well.
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#