使用stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding对URL编码NSString不编码@符号。解决这个问题的正确方法是什么?谢谢。如何对iPhone SDK中的@符号进行URL编码?
3
A
回答
10
的代码行,你已经有了后加入这个(并更改接收器等,以你自己的变量):
[escaped replaceOccurrencesOfString:@"@" withString:@"%40" options:NSCaseInsensitiveSearch range:wholeString];
如果你想确保一切进行编码,这里是最好的方法:
NSMutableString *escaped = [actionString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[escaped replaceOccurrencesOfString:@"&" withString:@"%26" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"+" withString:@"%2B" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"," withString:@"%2C" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"/" withString:@"%2F" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@":" withString:@"%3A" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@";" withString:@"%3B" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"=" withString:@"%3D" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"?" withString:@"%3F" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"@" withString:@"%40" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@" " withString:@"%20" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"\t" withString:@"%09" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"#" withString:@"%23" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"<" withString:@"%3C" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@">" withString:@"%3E" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"\"" withString:@"%22" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"\n" withString:@"%0A" options:NSCaseInsensitiveSearch range:wholeString];
7
到URL编码的最佳方式是与此,从我的博客文章http://simonwoodside.com/weblog/2009/4/22/how_to_really_url_encode/:
NSString * encodedString = (NSString *)CFURLCreateStringByAddingPercentEscapes(
NULL,
(CFStringRef)unencodedString,
NULL,
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8);
+1
上面的代码很好,但它缺少一些需要编码的字符。将此添加到代码..(CFStringRef)@“!*'\”();:@&= + $,/?%#[]%“ – AlBeebe 2011-02-18 06:26:14
相关问题
- 1. 对URL进行URL编码
- 2. 如何对BASE64进行URL编码?
- 3. 如何在C#中对字符串进行URL编码
- 4. 如何在ajax url中编码'+'符号
- 5. 对Python中的字符串进行URL编码和解码
- 6. URL编码+符号
- 7. 如何在JSP中对URL进行URL编码?
- 8. 如何编码符号的URL与jQuery
- 9. 如何在JSON.NET中对单引号/撇号进行编码
- 10. 如何在jQuery/JavaScript中对URL进行编码并在ASP.NET中进行解码
- 11. 如何避免对HTTP Url中的单引号进行编码修改?
- 12. 在Python中对unicode特殊字符(符号)进行编码?
- 13. 如何对DataGridView的行进行编号?
- 14. 如何对Twig中的HTML转义文本进行URL编码?
- 15. 对字符进行编码
- 16. 对Iphone Sdk中的NSMutableArray中的字符串进行排序?
- 17. 如何在java中对url字符串进行编码,以便不在空格中加上+符号?
- 18. 与符号编码,URL
- 19. 如何在iPhone中对横向进行编码?
- 20. 如何在iPhone中对横向进行编码?
- 21. 如何在Python中对字符串进行二进制编码?
- 22. 如何在PHP中正确地对URL进行编码?
- 23. 如何在angularJS中对URL进行编码
- 24. 如何避免在cronjob中对图像url进行硬编码?
- 25. 如何在php中使用preg_replace函数对url进行编码?
- 26. 符号URL编码发出
- 27. URL编码 - &符号问题
- 28. 在ASP中对英镑符号进行编码
- 29. 在java URI中对英镑符号进行编码
- 30. 在URL编码中对字典进行编码
打我吧。现在所有去投票我的原始答案:-) – 2009-06-06 10:53:21