Breaking News
Home / iOS / ObjectiveC / How to url encode a NSString in Objective C?

How to url encode a NSString in Objective C?

Answer

Encoding  parameters in query string or even the url path etc. is required otherwise the url become unusable. This can be achieved by using stringByAddingPercentEscapesUsingEncoding  method.

  1.     NSString *url = @"http://www.developerfeed.com/search.jsp?params=Java Developer";
  2.     NSString* encodedUrl =
  3.     [url stringByAddingPercentEscapesUsingEncoding:
  4.      NSASCIIStringEncoding];
  5.  
  6.    NSLog(@"Encoded URL %@",encodedUrl);
  7.    // output will be
  8.    //http://www.developerfeed.com/search.jsp?params=Java%20Developer;
[pb_builder]

Check Also

Singleton Example in Objective C?

Solution static MyManager *sharedManager = nil; +(MyManager*) sharedManager {         if (sharedManager == nil)         …

Leave a Reply