views:

459

answers:

2

I want to pass my textview content to a server via post request but I guess I have a problem with encoding. Do | have to convert say blanks to + or %20 by hand or is there a library supported by cocoa or any other 3rd party? Is this sth trivial ? I got stuck with this simple step for hours! :)

+3  A: 

Have a look at example how to add percent escapes at http://www.rudis.net/content/2009/01/26/adding-percent-esacpes-cocoa, quoting the author:

One obvious way to do this is by calling NSString's

-(NSString*)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)encoding

method. While this does a good job, it is not perfect and will miss things like "/", which may make some web services cry. The best way to do this encoding is to use:

CFStringRef CFURLCreateStringByAddingPercentEscapes (
   CFAllocatorRef allocator,
   CFStringRef originalString,
   CFStringRef charactersToLeaveUnescaped,
   CFStringRef legalURLCharactersToBeEscaped,
   CFStringEncoding encoding
);

Example code after jump (see link above).

stefanB
+1  A: 

Another example can be found at http://deusty.blogspot.com/2006/11/sending-http-get-and-post-from-cocoa.html. The example that stefanB linked to has a more complete list of characters that need escaping, though.

hjon
yeap, this link contains a neat and simple explanation, thanks
dkberktas
No problem. Be sure to look at stefanB's link for a more complete list of characters to include when escaping.
hjon