Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Question about character of NSString invalid in URL on iPhone

Some of my http request has 'space' in URL, but it is not recognized by iPhone. I have to change 'space' to '%20' whenever there is a 'space' in the url. Now I have to send a message typed by the user to the server and it can have as many 'spaces' as the user like. It looks like I have to replace them all.(stringByReplacingOccurrencesOfString)

Convert 'space' to '%20' is the one I occasionally find. My question is which of the other characters in NSString cannot be straightly used by url on iPhone?

like image 345
Chilly Zhong Avatar asked Mar 30 '09 03:03

Chilly Zhong


2 Answers

I think this is a better approach:

NSString* escapedUrl = [originalUrl   
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

If you want to understand how URL encoding works, take a look at this URL.

like image 83
Pablo Santa Cruz Avatar answered Nov 13 '22 05:11

Pablo Santa Cruz


See RFC 2396 for the full gory details. The following characters must be escaped:

Control characters (ASCII 00-1F and 7F)
Space
<
>
#
%
"

The following characters are unwise to use without escaping because some gateways and other transport agents are known to sometimes modify such characters, or they are used as delimiters:

{ } | \ ^ [ ] `

like image 22
Adam Rosenfield Avatar answered Nov 13 '22 04:11

Adam Rosenfield