‘&’, what a flow breaker. When you send a string data that contains ‘&’ in it to a server then what do you notice? In my case server received only data preceding ‘&’. What happened to the other data ? Well it seemed that data from ‘&’ onwards got cut away. In my previous post i mentioned how to encode string that contains spaces in it. I tried same encoding in this case and it didn’t work. I found a working solution, courtesy stackoverflow.com

I added a new routine function to our NSString via category, whose task was to encode the string so that special characters be handled gracefuly.

@interface NSString (RFC3875)
- (NSString *)stringByAddingRFC3875PercentEscapesUsingEncoding:(NSStringEncoding)encoding;
@end
@implementation NSString (RFC3875)
- (NSString *)stringByAddingRFC3875PercentEscapesUsingEncoding:(NSStringEncoding)encoding {
    CFStringEncoding cfEncoding = CFStringConvertNSStringEncodingToEncoding(encoding);
    NSString *rfcEscaped = (NSString *)CFURLCreateStringByAddingPercentEscapes(
                                                NULL, 
                                                (CFStringRef)self,
                                                NULL, 
                                                (CFStringRef)@";/?:@&=$+{}<>,",
                                                cfEncoding);
    return rfcEscaped;
}
@end

Everthing was smooth after that, just using above category to encode my input:

NSString *postDataString = [inputText stringByAddingRFC3875PercentEscapesUsingEncoding: NSUTF8StringEncoding];

Don’t forget to import:

#import "NSString+RFC3875.h"

Thanks.