如何将数据发送到服务器

时间:2014-01-10 14:10:23

标签: objective-c ios7

我想通过登录页面将数据发送到服务器。我正在给代码..

NSString *uname= txt_name.text;

//Here you place your URL Link
NSURL *theURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://localhost:8888/powerplay/insert2.php?name=%@",uname]]; 

NSURLRequest *req = [NSURLRequest requestWithURL:theURL];

NSURLConnection *connection = [NSURLConnection connectionWithRequest:req delegate:self];
if (connection) {
    NSLog(@"connection successful",uname);
}
else {
    NSLog(@"Failed");
}

通过上面的代码,我可以连接到服务器,但我真的无法将我的数据发布到我的Xcode控制台,通过它我可以看到我能够正确发送服务器。

1 个答案:

答案 0 :(得分:0)

如果您想查看从服务器返回的数据(也就是假设您的服务器正在发回一些数据),您已经将“self”设置为NSURLConnection的委托。

这意味着你所要做的就是实现the NSURLConnectionDelegate method connection:didReceiveData:",你可以将传入的数据打印到Xcode控制台。

例如:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    NSLog(@"received data %ld %@", [data length], [data description]);
}
相关问题