I would like to post data on server
with single URL
which contains voice data
, image and some text parameters using NSMutableURLRequest
and NSURLConnection
delegate
.
Thanks in advance.
NSString *dvcToken = [[NSUserDefaults standardUserDefaults] valueForKey:@"deviceToken"];
NSMutableDictionary* _params = [[NSMutableDictionary alloc] init];
// parameters to send
[_params setObject:@"842" forKey:@"office"];
[_params setObject:@"8442" forKey:@"client"];
[_params setObject:@"0" forKey:@"sub"];
[_params setObject:ofcrName forKey:@"name"];
[_params setObject:ofcrNo forKey:@"number"];// cin_out_datetime
[_params setObject:curntDateTime forKey:@"cin_out_datetime"];
if(logIn){
[_params setObject:@"I" forKey:@"process_type"];
logIn = false;
}else
[_params setObject:@"0" forKey:@"process_type"];
[_params setObject:dvcToken forKey:@"device_imei"];
[_params setObject:[[UIDevice currentDevice] name] forKey:@"device_name"];
//[_params setObject:audioData forKey:@"voice"];
// the boundary string : a random string, that will not repeat in post data, to separate post data fields.
NSString *BoundaryConstant = @"----------V2ymHFg03ehbqgZCaKO6jy";
// string constant for the post parameter 'file'. My server uses this name: `file`. Your's may differ
NSString* FileParamConstant = @"file";
// the server url to which the image (or the media) is uploaded. Use your server url here
NSURL* requestURL = [NSURL URLWithString:@"https://myUrl"];
// create request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:30];
[request setHTTPMethod:@"POST"];
// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", BoundaryConstant];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];
// post body
NSMutableData *body = [NSMutableData data];
// add params (all params are strings)
for (NSString *param in _params) {
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", [_params objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
}
// add image data
NSData *imageData = UIImageJPEGRepresentation(officerImage, 1.0);
if (imageData) {
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n", FileParamConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
// my audio to post
NSString *audioUrl = [NSString stringWithContentsOfURL:player.url];
NSData *audiodata;
audiodata = [[NSData alloc] initWithContentsOfFile:audioUrl];
// add it to body
[body appendData:audiodata];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// final boundary
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// set the content-length
NSString *postLength = [NSString stringWithFormat:@"%d", [body length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
// set URL
[request setURL:requestURL];