4

在我的代码中,我试图将一些数据发送到我的 PHP 文件

为了将东西添加到我的 SQL 中,我使用 GET 方法。

到目前为止,我需要在网站上使用表单,但我想通过浏览来添加数据,例如:

http://server.com/add.php?user=some data here&message= last data here

到目前为止,我正在尝试使用此代码:

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

 [messageView loadRequest:[NSURLRequest requestWithURL:add]];

然而 Xcode 告诉我:“方法调用的参数太多,预期 1,有 2”

4

3 回答 3

1

试试这个

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

你应该使用NSString +stringWithFormat:

于 2012-10-18T12:41:49.603 回答
0

URLWithString 接受字符串或字符串文字。你应该这样做:

NSString* urlString = [NSString stringWithFormat:@"http://server.com/ios/add.php?user=iPhone App&message=%@", messageBox.text];
NSURL *add = [NSURL URLWithString:urlString];  
[messageView loadRequest:[NSURLRequest requestWithURL:add]];
于 2012-10-18T12:43:51.510 回答
0
NSURL *add = [NSURL URLWithString:[NSString stringWithFormat:@"http://server.com/ios/add.php?user=iPhone App&message=%@", messageBox.text]];

这是因为 URLWithString: 只需要一个 NSString * 类型的参数。使用 NSString 的 +stringWithFormat: 方法从格式化的字符串和参数创建一个 NSString 对象。

于 2012-10-18T12:44:27.280 回答