11

到目前为止,我的发送按钮得到了这个代码:

NSString* urlString = [NSString stringWithFormat:@"http://server.com/ios/add.php?user=iPhone+App&message=%@", messageBox.text stringByAddingPercentEscapesUsingEncoding : NSUFT8StringEncoding];
NSURL *add = [NSURL URLWithString:urlString];

但是我确实收到错误“预期':'

4

5 回答 5

19

您需要将两个字符串构造调用分开

NSString* urlString = [[NSString stringWithFormat:@"http://server.com/ios/add.php?user=iPhone+App&message=%@", messageBox.text] stringByAddingPercentEscapesUsingEncoding : NSUTF8StringEncoding];
NSURL *add = [NSURL URLWithString:urlString];
于 2012-10-22T15:15:34.630 回答
7

正确的方法是不要转义整个 URL。您应该只转义单个参数值。

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];
于 2012-10-22T15:42:31.063 回答
1

尝试:

NSString* urlString = [[NSString stringWithFormat:@"http://server.com/ios/add.php?user=iPhone+App&message=%@", messageBox.text] stringByAddingPercentEscapesUsingEncoding : NSUFT8StringEncoding];
于 2012-10-22T15:15:44.757 回答
0

把 NSString 方法分开看清楚:

NSString* urlString = [[NSString stringWithFormat:@"http://server.com/ios/add.php?user=iPhone+App&message=%@", messageBox.text] stringByAddingPercentEscapesUsingEncoding : NSUFT8StringEncoding];
于 2012-10-22T15:18:58.627 回答
-1

当您调用 NSURL urlWithString 时,NSURL 将自动对字符串进行 url 编码,不需要此代码。

于 2012-10-22T17:19:32.713 回答