You can use stringByAddingPercentEscapesUsingEncoding:
NSString* escapedUrlString =
[unescapedString stringByAddingPercentEscapesUsingEncoding:
NSUTF8StringEncoding];
However, in my experience, this method isn't quite perfect (in handling some reserved characters), and in many cases I needed to use the variant:
NSString * escapedUrlString =
(NSString *)CFURLCreateStringByAddingPercentEscapes(
NULL,
(CFStringRef)unescapedString,
NULL,
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8 );
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…