// Вы можете управлять NSURLConnection синхронно, используя sendSynchronousRequest: returningResponse: error:
// но это заблокирует весь поток до получения ответа
// thebodyData = полезная нагрузка отправлена на сервер (в правильном формате)
// theMimeType = mineType полезной нагрузки
// url - это подходящий URL для вызова http POST
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
[theRequest setHTTPMethod:@"POST"];
NSURLConnection *theConnection = [[NSURLConnection alloc]initWithRequest:theRequest delegate:self];
if(theConnection)
{
webData = [[NSMutableData data]retain];
// give the details of the payload -- mine time and body content.
[theRequest setValue: theMimeType forHTTPHeaderField:@"Content-Type"];
[theRequest setHTTPBody:theBodyData];
}
else
{
NSLog(@"theConnection is NULL");
}
// the delegate methods templates...
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[webData setLength:0]; // clear the data incase it was a redirect in between.
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webData appendData:data]; // collect the data from server as it comes in.
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[[NSAlert alertWithError:error] runModal]; // report the error
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// Once this method is invoked, "webData" contains the complete result
}