8

I'm using this code snippet to encode characters to be friendly with a POST request:

   NSString *unescaped = [textField text];
   NSString *escapedString = (__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes(
                                                                                  NULL,
                                                                                  (__bridge_retained CFStringRef)unescaped,
                                                                                  NULL,
                                                                                  (CFStringRef)@"!*'();:@&=+$,/?%#[]",
                                                                                  kCFStringEncodingUTF8);

Which works great, but does not add escape Quotation marks: "

How do I escape Quotation marks in IOS?

Moe
  • 4,744
  • 7
  • 28
  • 37

4 Answers4

16

I needed to take the user inputted NSString from [textField text] and make sure that if there are quotation marks in the string, they are escaped properly in order to send through a POST statement.

My solution was:

unescaped = [unescaped stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Moe
  • 4,744
  • 7
  • 28
  • 37
4

First, you don't want to use __bridge_retained in your cast to a CFStringRef. Just use __bridge.

Second, you don't have to escape the quotes manually by string replacement. Just add the quote character to the set of characters to be quoted when calling CFURLCreateStringByAddingPercentEscapes(). Like so:

NSString *unescaped = [textField text];
NSString *escapedString = CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,
                                                                                    (__bridge CFStringRef)unescaped,
                                                                                    NULL,
                                                                                    CFSTR("!*'();:@&=+$,/?%#[]\""),
                                                                                    kCFStringEncodingUTF8));

(In addition to adding the quote to the set, I changed to use CFBridgingRelease() rather than a __bridge_transfer cast because I find it clearer. It satisfies the feeling that all CF "Create" functions need a corresponding "Release". Also, I changed the use of a @"" literal cast to CFStringRef to just a CFSTR("") literal.)

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
3

Quotes are to be escaped with \".

As in:

(CFStringRef)@"I'm an \"example\""
CodaFi
  • 43,043
  • 8
  • 107
  • 153
  • I know this, I want you to know how to get a user inputted `"` and change it to: `\"` – Moe Apr 12 '12 at 09:19
0

Try using \" instead of using " directly...

Special Characters like Quotes, slashes and others require \ to make that character to remove its special functionality.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
adi27
  • 509
  • 1
  • 3
  • 10