-1

I want to make a POST request on objective c using NSURL. I've communicated to the desired website with coding like this, however it is only for text.

    NSDictionary *parameters = @{
                             @"data": text.text,
                             };

NSMutableString *parameterString = [NSMutableString string];
for (NSString *key in [parameters allKeys]) {
    if ([parameterString length]) {
        [parameterString appendString:@"&"];
    }
    [parameterString appendFormat:@"%@=%@", key, parameters[key]];
}

NSURLSession *session = [NSURLSession sharedSession];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://~~~"]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[parameterString dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    if (!error) {
        if ([data length]) {
            NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            //[liberal setText:[NSString stringWithFormat:@"%@", @"results"]];
            NSLog(@"A: %@", jsonResponse[@"results"][@"A"]);

    } else {
        NSLog(@"%@", error);
    }
}];
[task resume];

}

However, I was wondering if there was a similar method I could use for posting an image following these lines?

Basically I need to upload an image and retrieve a number from it. (after it has been analysed)

MasterRazer
  • 1,377
  • 3
  • 16
  • 40
robert
  • 131
  • 1
  • 9

1 Answers1

2

If you would like to upload image using NSMutableURLRequest, you need to build multipart form data yourself.

You can find a good example here: NSData and Uploading Images via POST in iOS

But it will be much more simple, if you will use AFNetworking. Example could be found here: iOS Image upload via AFNetworking 2.0

Community
  • 1
  • 1
Andrey
  • 2,659
  • 4
  • 29
  • 54