Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSURL Encoding in ObjC

Tags:

cocoa

How to do URL encoding in NSURL ?

Thanks

like image 739
Biranchi Avatar asked Nov 17 '09 13:11

Biranchi


1 Answers

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 );
like image 180
notnoop Avatar answered Dec 29 '22 04:12

notnoop