Я превратил это в простую функцию, которая может быть включена и использована во всех проектах.В моем случае удаленный сервер - это PHP-скрипт.
Вот как используется функция:
// form text fields and values
NSArray *formfields = [NSArray arrayWithObjects:@"name", @"date", @"purpose", @"comment", nil];
NSArray *formvalues = [NSArray arrayWithObjects:@"Write Blog", @"31-03-2011", @"For Test", @"Just Comment", nil];
NSDictionary *textParams = [NSDictionary dictionaryWithObjects:formvalues forKeys:formfields];
// if, there are any images in the form
NSArray *images = [NSArray arrayWithObjects:@"myimage1.png", @"myimage2.png", nil];
// submit the form
[self doPostWithText:textParams andImage:images];
Это определение функции для doPostWithText: andImage:
- (void) doPostWithText:(NSDictionary *)textParams andImage:(NSArray *)imageParams
{
NSString *urlString = @"http://mysite.com/myscript.php";
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSMutableData *body = [NSMutableData data];
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
// add the image form fields
for (int i=0; i<[imageParams count]; i++) {
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: attachment; name=\"image%d\"; filename=\"%@\"\r\n", i, [imageParams objectAtIndex:i]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:UIImageJPEGRepresentation([UIImage imageNamed:[imageParams objectAtIndex:i]], 90)]];
[body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
// add the text form fields
for (id key in textParams) {
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:[textParams objectForKey:key]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
// close the form
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// set request body
[request setHTTPBody:body];
// send the request (submit the form) and get the response
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"%@", returnString);
}
На стороне PHP я получу такие данные:
$_POST
Array
(
[name] => Write Blog
[purpose] => For Test
[comment] => Just Comment
[date] => 31-03-2011
)
$_FILES
Array
(
[image0] => Array
(
[name] => myimage1.png
[type] => application/octet-stream
[tmp_name] => /junk/temp/phpsKiVxx
[error] => 0
[size] => 23602
)
[image1] => Array
(
[name] => myimage2.png
[type] => application/octet-stream
[tmp_name] => /junk/temp/phpNVGDoB
[error] => 0
[size] => 4620
)
)