title: String encode in objective c
body: <p>I am very new to Objective-C.</p>
  
  <p>I want to get the encoded content for a NSString. In java I can do that as follows,</p>
  
  <pre><code>String str = "https://www.google.co.in/#q=ios+sqlite+crud+example";
  String encodedParam = URLEncoder.encode(str, "UTF-8");
  </code></pre>
  
  <p>I am using <a href="http://www.tutorialspoint.com/compile_objective-c_online.php" rel="nofollow">http://www.tutorialspoint.com/compile_objective-c_online.php</a> to test the codes posted in stackoverflow. There is no solution yet. I know its trivial one. Struggling to find a way though.</p>
  
  <p>tried with following function, and it says following error while compile,</p>
  
  <pre><code>-(NSString *)urlEncodeUsingEncoding:(NSStringEncoding)encoding {
  return (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,
         (CFStringRef)self,
         NULL,
         (CFStringRef)@"!*'\"();:@&amp;=+$,/?%#[]% ",
         CFStringConvertNSStringEncodingToEncoding(encoding));
  </code></pre>
  
  <p>}</p>
  
  <p>Error,</p>
  
  <pre><code>    sh-4.3$ gcc `gnustep-config --objc-flags` -L/usr/GNUstep/System/Library/Libraries -lgnustep-base -lobjc *.m -o main                                                             
      main.m: In function 'main':                                                                                                                                                     
      main.m:7:14: error: 'urlEncodeUsingEncoding' undeclared (first use in this function)                                                                                            
       -(NSString *)urlEncodeUsingEncoding:(NSStringEncoding)encoding {                                                                                                               
                    ^                                                                                                                                                                 
      main.m:7:14: note: each undeclared identifier is reported only once for each function it appears in                                                                             
      main.m:7:36: error: expected ';' before ':' token                                                                                                                               
       -(NSString *)urlEncodeUsingEncoding:(NSStringEncoding)encoding {   
  </code></pre>
  
  <p>Edit as per the answers,</p>
  
  <p>Suggested by Patrick, I used the code as follows,</p>
  
  <pre><code>NSString *storedURL = @"google.com/?search&amp;q=this";
  NSString *urlstring = [NSString stringWithFormat:@"http://%@/",storedURL];
  NSURL *url = [NSURL URLWithString:urlstring];
  
  NSError *error = nil;
  NSStringEncoding encoding;
  NSString *my_string = [[NSString alloc] initWithContentsOfURL:url
                                                   usedEncoding:&amp;encoding 
                                                          error:&amp;error];
  NSLog (my_string);
  </code></pre>
  
  <p>Nothing printed in console... Is it my NSLog is right?</p>
  
  <p>Suggested by lightwolf, my code is looks like below,</p>
  
  <pre><code>NSString *str = @"https://www.google.co.in/#q=ios+sqlite+crud+example";
  NSString *encodedParam = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  NSLog (encodedParam);
  </code></pre>
  
  <p>it prints the log, but value is same as the str..... not encoded... I want this str as </p>
  
  <pre><code>https%3A%2F%2Fwww.google.co.in%2F%23q%3Dios%2Bsqlite%2Bcrud%2Bexample
  </code></pre>