I am posting text with ampersand "&" back to web service and it is causing issues in my Objective C application.
I have to escape it somehow.
Any ideas?... I am very new to Objective C and iPhone programming at the moment...
This happens when you try to pass some values in the
URL and the value contains
& which is used to separate parameters in the
URL, hence the web server spilts the content into parts wherever it encounters an
& rather than taking it as a single continuos value.
You can url encode the string to resolve the problem.
NSString *valueToSubmit = @"This is my value which contains & in it."; NSString *urlEncodedString = (NSString*)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,(CFStringRef)valueToSubmit,NULL,(CFStringRef)@"!*'();:@&=+$,/?%#",kCFStringEncodingUTF8)); NSLog(@"Non-Encoded: %@ \n Encoded: %@",valueToSubmit,urlEncodedString];
The output will be something like this:
Non-Encoded: This is my value which contains & in it. URL Encoded: This%20is%20my%20value%20which%20contains%20%26%20in%20it.