将UITextView / UITextField中的空格编码为URL格式 [英] Encoding spaces in UITextView / UITextField to URL format
问题描述
我正在尝试将UITextView或UITextField的内容作为参数发送到php文件
I'm trying to send the contents of UITextView or UITextField as parameters to a php file
NSString *urlstr = [[NSString alloc] initWithFormat:@"http://server.com/file.php?name=%@&tags=%@&entry=%@",nameField.text, tagsField.text, dreamEntry.text];
当我记录urlstr时,只要UITextView或UITextField没有,url格式就可以了包含空格。我如何将空格转换为%20?
When i log urlstr, the url format is ok just as long as the UITextView or UITextField don't contain spaces. How would i go about converting the spaces to %20 ?
编辑
这里是目前的代码,它不仅崩溃,而且还没有正确编码网址。
here is the code at present, which not only crashes but isn't encoding the url properly.
name = John Doe& tags = reurring nightmare& entry =测试测试测试
name=John Doe&tags=recurring nightmare&entry=Testing testing testing
转换为
name = John -1844684964oe& tags = recurringightmare& entry = Testing 4.214929e-307sting -1.992836e + 00sting
name=John -1844684964oe&tags=recurringightmare&entry=Testing 4.214929e-307sting -1.992836e+00sting
- (IBAction)sendButtonPressed:(id)sender
{
NSString *urlString = [[NSString alloc] initWithFormat:@"http://server.com/file.php?name=%@&tags=%@&entry=%@", nameField.text, tagsField.text, dreamEntry.text];
NSString *encodedString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [[NSURL alloc] initWithString:encodedString];
NSLog(encodedString);
NSLog(urlString);
[urlString release];
[url release];
[encodedString release];
}
推荐答案
你不应该对整个字符串进行URL转义,你应该对动态组件进行URL转义。尝试
You're not supposed to URL-escape the entire string, you're supposed to URL-escape the dynamic components. Try
NSString *urlStr = [NSString stringWithFormat:@"http://server.com/file.php?name=%@&tags=%@&entry=%@",
[nameField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[tagsField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[dreamEntry.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
nil];
NSURL *url = [NSURL URLWithString:urlStr];
你的代码的第二个问题(毫无疑问是打印奇数的原因)是你要经过字符串直接到NSLog,因此它被视为格式字符串。你需要使用
The second issue with your code (and undoubtedly the reason for the odd printing) is you're passing the string directly to NSLog, so it's being treated as a format string. You need to use
NSLog(@"%@", encodedString);
。这将使它按预期打印。
instead. That will make it print as expected.
编辑:你的代码的第三个问题是你混合自动释放和拥有的对象,然后在最后释放它们。去看看你创建的3个对象,以及随后发布的对象。其中一个不应该在以后发布,因为它是由一个不以alloc,copy或new开头的方法生成的。识别有问题的对象是留给读者的一个练习。
A third issue with your code is you're mixing autoreleased and owned objects, then releasing them all at the end. Go look at the 3 objects you create, and which you subsequently release later. One of them shouldn't be released later because it was produced by a method that did not start with the words alloc, copy, or new. Identifying the object in question is an exercise left to the reader.
这篇关于将UITextView / UITextField中的空格编码为URL格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!