关于iPhone上URL的NSString字符无效的问题 [英] Question about character of NSString invalid in URL on iPhone
问题描述
我的某些http请求在网址中有空格,但iPhone无法识别。只要网址中有空格,我就必须将空格更改为%20。现在我必须将用户键入的消息发送到服务器,它可以拥有与用户一样多的空格。看起来我必须全部替换它们。(stringByReplacingOccurrencesOfString)
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)
将'space'转换为'%20'是我偶尔会找到的。我的问题是,iPhone上的网址不能直接使用NSString中的哪些其他字符?
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?
推荐答案
我认为这是更好的方法:
I think this is a better approach:
NSString* escapedUrl = [originalUrl
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
如果您想了解URL编码的工作原理,请看一下网址。
If you want to understand how URL encoding works, take a look at this URL.
这篇关于关于iPhone上URL的NSString字符无效的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!