0

Actually i have json parameter

[dictionary setObject:_dateOfBirth.text  forKey:@"birth_date"];
    [dictionary setObject:_tfCountry.text  forKey:@"country"];
    [dictionary setObject:_tfEmail.text  forKey:@"email"];
    [dictionary setObject:@""  forKey:@"fromLogin"];
    [dictionary setObject:@1  forKey:@"gender"];
    [dictionary setObject:@"signup"  forKey:@"methodName"];
    [dictionary setObject:_tfContact.text  forKey:@"mobile"];
    [dictionary setObject:_tfName.text  forKey:@"name"];
    [dictionary setObject:_tfNickName.text  forKey:@"nickname"];
    [dictionary setObject:_tfPassword.text  forKey:@"password"];
    [dictionary setObject:_tfPinCode.text  forKey:@"pincode"];

There Is also a Image that i have to set profile_pic as a Key. Now I have Converted all Parameter as a data and POst That Data lie This

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:kNilOptions error:nil];

    // this is your service request url

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://proteen2.inexture.com/webservice"]];

    // set the content as format

    [request setHTTPMethod:@"POST"];

    [request setHTTPBody: jsonData];

    // this is your response type

    [request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"content-type"];

    NSError *err;
    NSURLResponse *response;

    // send the synchronous connection

    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];

    // here add your server response NSJSONSerialization

    NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:responseData options: NSJSONReadingMutableContainers error: &err];

It's Works fine for Text, Now how to attach Image to that Parameter, I am only aware with Multi Part but not getting that Point. Please Help

Santo
  • 1,611
  • 3
  • 17
  • 37
Mad Burea
  • 531
  • 8
  • 22
  • check: http://stackoverflow.com/questions/8042360/nsdata-and-uploading-images-via-post-in-ios – Nitin Gohel Jun 09 '16 at 07:37
  • What is Boundary? if i convert image into NSdata the How To add That data into reference to That Parameter to Particular key. I m confused – Mad Burea Jun 09 '16 at 07:41

1 Answers1

0

You need to manage many thing here, like set boundry append image data etc etc.

You should use AFNetworking to make is very simple. Download from github and just drag and drop library to your project and import AFNetworking.h in your class and then you can do something like,

   NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer]multipartFormRequestWithMethod:@"POST" URLString:@"urlstring" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {

    //Append image here for example;

    UIImage *img = tempImageView.image;
    NSData *imgData = UIImageJPEGRepresentation(img, 0.5);

    [formData appendPartWithFileData:imgData name:@"imagename/serversideparameter" fileName:@"imagename" mimeType:@"image/jpeg"];


} error:nil];

//Send this request to server. Something like,

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];

AFURLSessionManager *manager = [[AFURLSessionManager alloc]initWithSessionConfiguration:configuration];

[[manager dataTaskWithRequest:request completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {

    if (!error) {

        NSLog(@"success!!");

        NSLog(@"here is the response : %@",responseObject);
    }
    else{

        NSLog(@"Error Occured : %@",error.localizedDescription);
    }

}]resume];

You shouldn't use NSUrlConnection because it is deprecated now. It's better to use NSUrlSession which i have used in answer via AFNetworking.

If you don't want to use AFNetworking then refer this stackoverflow post. It have great explanation step by step in answer.

Community
  • 1
  • 1
Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75