到目前为止我的发送按钮已获得此代码:
NSString* urlString = [NSString stringWithFormat:@"http://server.com/ios/add.php?user=iPhone+App&message=%@", messageBox.text stringByAddingPercentEscapesUsingEncoding : NSUFT8StringEncoding];
NSURL *add = [NSURL URLWithString:urlString];
但是我确实收到错误“预期”:'
答案 0 :(得分:19)
您需要分开两个字符串构造调用
NSString* urlString = [[NSString stringWithFormat:@"http://server.com/ios/add.php?user=iPhone+App&message=%@", messageBox.text] stringByAddingPercentEscapesUsingEncoding : NSUTF8StringEncoding];
NSURL *add = [NSURL URLWithString:urlString];
答案 1 :(得分:7)
正确的方法是不要逃避整个网址。您应该只转义单个参数值。
NSString* urlString = [NSString stringWithFormat:@"http://server.com/ios/add.php?user=iPhone+App&message=%@", [messageBox.text stringByAddingPercentEscapesUsingEncoding : NSUFT8StringEncoding]];
更好的是,为了便于阅读和调试,请执行以下操作:
NSString *escapedText = [messageBox.text stringByAddingPercentEscapesUsingEncoding:NSUFT8StringEncoding];
NSString* urlString = [NSString stringWithFormat:@"http://server.com/ios/add.php?user=iPhone+App&message=%@", escapedText];
答案 2 :(得分:1)
尝试:
NSString* urlString = [[NSString stringWithFormat:@"http://server.com/ios/add.php?user=iPhone+App&message=%@", messageBox.text] stringByAddingPercentEscapesUsingEncoding : NSUFT8StringEncoding];
答案 3 :(得分:0)
分开NSString方法以清楚地看到它:
NSString* urlString = [[NSString stringWithFormat:@"http://server.com/ios/add.php?user=iPhone+App&message=%@", messageBox.text] stringByAddingPercentEscapesUsingEncoding : NSUFT8StringEncoding];
答案 4 :(得分:-1)
NSURL会在你调用NSURL urlWithString时自动对字符串进行url编码,不需要这段代码。