Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending an amp (&) using a post with Obj-C

I am sending a post containing text, numbers and data. The numbers and data work fine, but I'm having problems with the text, since it may contain an ampersand (&). For example

page.php?text=Hello World & Space.

Now I found that the "&" is received by server, but read as if a new variable starts. So it sees (I think):

text = "Hello World "
Space. =

I did read that I could try to encode the text to make it look like it's a URL (like " " [space] turns into "%20") but there is no way to encode it properly. I came to the conclusion:

textToPOST = [text stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];

But that does NOT encode the ampersands, but everything else. So the result is:

some text ü blablabla

turns into

some%20text%20ü%20blablabla

with the & not encoded. So how can I do this, please help.

Thanks a lot already

like image 884
Max Z. Avatar asked Jul 31 '11 10:07

Max Z.


People also ask

What is AMP in email?

AMP for Email allows senders to include AMP components inside rich engaging emails, making modern app functionality available within email.

What is an AMP message?

AMP email is a technology that allows email marketers to embed the interactive elements like carousels, accordions, confirmation, purchase buttons, etc., into their emails without needing to open a new tab to visit a website.

How do I send an AMP through Gmail?

Confirm Sender Addresses in Gmail Go to the "Other settings" tab in the service settings. In the "Email AMP" section, click Create Request. In the "Request to validate sender address" form, select the email address from which you want to send the campaign.


2 Answers

Unfortunately stringByAddingPercentEscapesUsingEncoding is not URL-encoding as most people understand it. It escapes characters that are completely invalid to have in a URL, but not characters that have a special meaning in a URL. So it's usable for ‘correcting’ an invalid user-inputted URL, but it's quite unsuitable for creating a URL from escaped path and query string components.

(In JavaScript terms, it is analogous to encodeURI and not the one you need far more often which is encodeURIComponent.)

So to do it properly you will need another method, either your own or pinched from an existing framework (this is a very common sore point so many libraries have workarounds). It's possible to implement using CFURLCreateStringByAddingPercentEscapes. See this answer for example code.

like image 113
bobince Avatar answered Nov 15 '22 18:11

bobince


Thanks for that answer and sorry for my premature posting :S I hope it at least helps anybody who has the same issue as I had. I just had a chat with a friend and he told me:

To display and ampersand in XML simply use the & quote. Unlike with a pure "&", the NSXMLParser won't output an error. Using & also allows you to make other symbol escapes like:

  • ü [in text] -> ü [in HTML] -> ü [in XML]
  • Ä [in text] -> Ä [in HTML] -> Ä [in XML]
  • ß [in text] -> ß [in HTML] -> ß [in XML]
  • etc...

So that's how you solve the "How do I parse XML with an amp in it problem". Also I managed to make the code, from the website I linked in a comment to my original question, work:

NSMutableString *deutschEscaped = [NSMutableString stringWithString:[[deutschTextLabel string] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[deutschEscaped replaceOccurrencesOfString:@"$" withString:@"%24" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [deutschEscaped length])];
[deutschEscaped replaceOccurrencesOfString:@"&" withString:@"%26" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [deutschEscaped length])];
[deutschEscaped replaceOccurrencesOfString:@"+" withString:@"%2B" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [deutschEscaped length])];
[deutschEscaped replaceOccurrencesOfString:@"," withString:@"%2C" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [deutschEscaped length])];
[deutschEscaped replaceOccurrencesOfString:@"/" withString:@"%2F" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [deutschEscaped length])];
[deutschEscaped replaceOccurrencesOfString:@":" withString:@"%3A" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [deutschEscaped length])];
[deutschEscaped replaceOccurrencesOfString:@";" withString:@"%3B" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [deutschEscaped length])];
[deutschEscaped replaceOccurrencesOfString:@"=" withString:@"%3D" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [deutschEscaped length])];
[deutschEscaped replaceOccurrencesOfString:@"?" withString:@"%3F" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [deutschEscaped length])];
[deutschEscaped replaceOccurrencesOfString:@"@" withString:@"%40" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [deutschEscaped length])];
[deutschEscaped replaceOccurrencesOfString:@" " withString:@"%20" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [deutschEscaped length])];
[deutschEscaped replaceOccurrencesOfString:@"\t" withString:@"%09" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [deutschEscaped length])];
[deutschEscaped replaceOccurrencesOfString:@"#" withString:@"%23" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [deutschEscaped length])];
[deutschEscaped replaceOccurrencesOfString:@"<" withString:@"%3C" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [deutschEscaped length])];
[deutschEscaped replaceOccurrencesOfString:@">" withString:@"%3E" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [deutschEscaped length])];
[deutschEscaped replaceOccurrencesOfString:@"\"" withString:@"%22" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [deutschEscaped length])];
[deutschEscaped replaceOccurrencesOfString:@"\n" withString:@"%0A" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [deutschEscaped length])];

This works fine for me and does replace ampersands and any other possible hazards.

If you don't want many %20 escapes for every space, you can also use:

NSMutableString *englishEscaped = [NSMutableString stringWithString:[[englishTextLabel string] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

Thank you all :)

like image 39
Max Z. Avatar answered Nov 15 '22 20:11

Max Z.