Developer Forums relocated!

Need help with Apple Developer tools and technologies? Want to share information with other developers and Apple engineers? Visit Developer Forums at Apple.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

NSString with special characters to NSURL

Hello..!

I am looking for the right answer to solve my "little problem". I just need to convert my NSString (it is the text of a message having special charcters like &, <, and so on) to add it as parameters to my NSURL request. as long as there are no special characters, I dont have any problem.. But if, the nserror says BAD URL.

I know that there are many threads about this issue, but, why-ever, I cant find the "answer" to my problem.


NSString * encodedParam = [dontEncodedString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];


does not solve my problem..

macbook air, Mac OS X (10.5.5)

Posted on Nov 26, 2009 9:48 AM

Reply
Question marked as Best reply

Posted on Dec 1, 2009 12:40 AM

sommeralex wrote:
... I just need to convert my NSString (it is the text of a message having special charcters like &, <, and so on) to add it as parameters to my NSURL request. ...
NSString *encodedParam =
[dontEncodedString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

does not solve my problem..

Do you mean the text needs to be part of the URL, or is it added to the body of the request? stringByAddingPercent.. is the correct method for cleaning text embedded in a URL. But if the text is going elsewhere, the special chars need to be handled differently.

It might be helpful if you posted an example of the raw text which is causing the problem, along with the details of where the text is going. In other words, if the text will be part of the URL, show us how the text started out and what the resulting URL looks like. Please add lots of NSLog() statements to your code as well. Try to log the text after each step so we can see what's going on.
- Ray
7 replies
Question marked as Best reply

Dec 1, 2009 12:40 AM in response to sommeralex

sommeralex wrote:
... I just need to convert my NSString (it is the text of a message having special charcters like &, <, and so on) to add it as parameters to my NSURL request. ...
NSString *encodedParam =
[dontEncodedString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

does not solve my problem..

Do you mean the text needs to be part of the URL, or is it added to the body of the request? stringByAddingPercent.. is the correct method for cleaning text embedded in a URL. But if the text is going elsewhere, the special chars need to be handled differently.

It might be helpful if you posted an example of the raw text which is causing the problem, along with the details of where the text is going. In other words, if the text will be part of the URL, show us how the text started out and what the resulting URL looks like. Please add lots of NSLog() statements to your code as well. Try to log the text after each step so we can see what's going on.
- Ray

Dec 5, 2009 5:08 AM in response to RayNewbie

Hi Ray!

Thank you for your answer!

My String I want to pass (with json) is
{"lat":"48","long":"16","text":"!@&"}

the generated text is
&message=%7B%22lat%22:%2248%22,%22long%22:%2216%22,%22text%22:%22%3C%3E!@&%22%7D

the method I am using:

NSString *codeMessage = [message stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
tempString = [tempString stringByAppendingFormat:@"&message=%@", codeMessage];
tempString = [tempString stringByReplacingOccurrencesOfString:@" " withString:@""];

Dec 5, 2009 7:40 PM in response to sommeralex

sommeralex wrote:
My String I want to pass (with json) is
{"lat":"48","long":"16","text":"!@&"}

the generated text is
&message=%7B%22lat%22:%2248%22,%22long%22:%2216%22,%22text%22:%22%3C%3E!@&%22%7D

the method I am using:

NSString *codeMessage = [message stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
tempString = [tempString stringByAppendingFormat:@"&message=%@", codeMessage];
tempString = [tempString stringByReplacingOccurrencesOfString:@" " withString:@""];

It fairly clear that stringByAddingPercentEscapesUsingEncoding is NOT converting the "&" near the end of your initial string. That means the final result looks like &message=xxx&yyy which is probably not what you want.

How is the original string created, and do the special characters at the end change, or mean something as far as an action that should be taken?

Obviously this is some kind of location, followed by a text field.
If the characters other than the lat/long ("!@&") are static, then I would encode them once, by hand to insure the & is encoded. Another option, if you do not know whether the & is going to appear in the text, is to do:

NSString *codeMessage = [message stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
codeMessage = [codeMessage stringByReplacingOccurrencesOfString:@"&" withString:@"%26"];
tempString = [tempString stringByAppendingFormat:@"&message=%@", codeMessage];
tempString = [tempString stringByReplacingOccurrencesOfString:@" " withString:@""];

Obviously the first string substitution needs to be done BEFORE you add the text to "&message=xxx". It's understandable that "&" is not substituted, since you could have "&message=foo&text=bar&etc=more". Maybe Ray or someone else knows if there is a better method to call to change the special characters to hex characters.

However, if this is the only character not getting converted, the extra replacement should solve the problem.

Dec 6, 2009 1:07 PM in response to reststop

thank you for your helpful answers. the point is that I dont know, WHAT SPECIAL CHARACTERS I do have to take care of.

The text could have all special characters, which are provided by the keyboard, but I dont know which characters I have to substitute with %xyz.


stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding

does something, but not everything..

I guess there is somewhere a list for hex codes? as well as for all special characters I have to take care of?

1.
why is & substituted by %26
2.
why is if i am writing %26 in my text, this not recognized or translated as a &? (i am happy that it is not translated, but what is the difference between a hardcoded %26 and a translated %26 (from &)


thanks..

Dec 6, 2009 11:59 PM in response to sommeralex

sommeralex wrote:
I think, i am on the way, but still with some stones. For example, I would like to use the € symbol, and the hex list shows 20AC as the code. but 20AC does not work, nor %20AC.


No, that won't work, because you are expecting U+20AC (Unicode) to be encoded as UTF-8 or straight ascii. 0x20AC is the hexadecimal unicode value. %20AC is %20 (a space) followed by AC.

The routine you are using to translate characters, is trying to make them web ready, and there may be a % substitution that will encode ALL characters. I would pull up the documentation page for both the substitution methods, and the % encoding and read the information keeping an eye out for how to do other encodings.

There may well be a UFT-8 encoding for the EURO symbol. I don't know. The encoding you found on the web is for Unicode in general.

The pages I found mentioned a "numerical HTML encoding of the Unicode character" which says to use "&#20AC;" but, that is for encoding it in an HTML document, so it will be displayed properly just like using " " for a non-breaking space.

That doesn't describe how to use if for a HTTP protocol which is what you use to send messages between web servers and other servers.

If you are not going to send the euro symbol via your JSON connection, then it's not a problem.

Hope this helps, even if it doesn't solve your euro issue.
The previous code I gave should work for your & issue.

-Carl

NSString with special characters to NSURL

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.