0

I am uploading the image to server by this code. It works fine in iOS deployment target 8.1 version but when I try to change the deployment target to 9.1 version the return data is getting null because [NSURLConnection sendSynchronousRequest:request1 returningResponse:nil error:nil]; is deprecated in 9.0.

Here is the issue.

sendAsynchronousRequest:queue:completionHandler:' is deprecated: first deprecated in iOS 9.0 - Use [NSURLSession dataTaskWithRequest:completionHandler:] (see NSURLSession.h).

But I am unable to do this using NSURLSession. Please help to modify this code to NSURLSession.

NSData *imageData = UIImageJPEGRepresentation(cameraImageViewOutlet.image, compression);

while ([imageData length] > maxFileSize && compression > maxCompression)
{
    compression -= 0.1;
    imageData = UIImageJPEGRepresentation(cameraImageViewOutlet.image, compression);

}

// Here is the uploading the image to server by POST
NSString *urlString1 = @"bfkgfdkhkghghj";

NSMutableURLRequest *request1 = [[NSMutableURLRequest alloc] init];
[request1 setURL:[NSURL URLWithString:urlString1]];
[request1 setHTTPMethod:@"POST"];

NSMutableData *body = [NSMutableData data];

// Here we are doing the image to multipart...
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request1 addValue:contentType forHTTPHeaderField:@"Content-Type"];

// sending the image parameter in (name=\"img_file[]\";) passed given parameter...
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: attachment; name=\"img_file[]\"; filename=\".jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

//sending the parameter(name=\"contentTypeId\) passed given parameter...
NSString *param1 = @"1";
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"contentTypeId\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:param1] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

// close form
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

// set request body
[request1 setHTTPBody:body];

//return and test
NSData *returnData = [NSURLConnection sendSynchronousRequest:request1 returningResponse:nil error:nil];

NSMutableDictionary * uploadData = [NSJSONSerialization JSONObjectWithData:returnData options:0 error:nil];
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Sudhakar Varma
  • 173
  • 1
  • 6
  • See http://stackoverflow.com/questions/31219548/deprecated-sendasynchronousrequestqueuecompletionhandler-in-ios9 – rmaddy Nov 29 '15 at 03:38
  • I have gone through the above ( stackoverflow.com/questions/31219548/…) but am unable to understand in my scenario. when am writing [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:ourBlock]; this line ourBlock showing error please help out in my scenario. – Sudhakar Varma Nov 29 '15 at 06:50

0 Answers0