Sending an amplifier (&) using a message from Obj-C

I am sending a message containing text, numbers and data. Numbers and data work fine, but I'm having problems with the text, as it may contain an ampersand (&). for instance

page.php?text=Hello World & Space. 

Now I found that "&" was received by the server, but it reads as if a new variable was starting. So he sees (I think):

 text = "Hello World " Space. = 

I read that I can try to encode the text so that it looks like a URL (for example, "[space] turns into"% 20 "), but there is no way to encode it correctly. I came to the conclusion:

 textToPOST = [text stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]; 

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

 some text ü blablabla 

turns into

 some%20text%20ü%20blablabla 

c and not encoded. So how can I do this, please help.

Thank you very much already

+6
source share
2 answers

Thanks for this answer and sorry for my premature posts: S I hope this at least helps anyone who has the same problem as mine. I just chatted with a friend and he told me:

To display and ampersand in XML just use & quotes. Unlike pure "&", NSXMLParser does not throw an error. Using & also allows you to do other escape sequences of characters, for example:

  • & uuml; [in text] → & uuml; [in HTML] → & uuml; [in XML]
  • & Auml; [in text] → & Auml; [in HTML] → & Auml; [in XML]
  • & szlig; [in text] → & szlig; [in HTML] → & szlig; [in XML]
  • etc.

So, how do you decide "How to parse XML with an amplifier in it." I also managed to make a code, from the site that I linked in the comments to my original question, the 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 great for me and replaces ampersands and any other possible dangers.

If you do not want to have only 20 screens for each space, you can also use:

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

Thanks everyone :)

+1
source

Unfortunately, stringByAddingPercentEscapesUsingEncoding not a URL encoding, as most people understand. It avoids characters that are completely invalid for the URL, but not characters that have special meaning in the URL. Thus, it can be used to correct an invalid URL entered by the user, but it is completely unsuitable for creating a URL from escaped paths and query string components.

(In terms of JavaScript, it is similar to encodeURI , not the one you need more often than encodeURIComponent .)

To do this correctly, you will need another method - your own or infringed from the existing structure (this is a very common problem, so many libraries have workarounds). Can be implemented using CFURLCreateStringByAddingPercentEscapes . See this answer for example code.

+6
source

Source: https://habr.com/ru/post/893975/


All Articles