0

I am currently building an app in Xcode using Objective C and I need to post two text variables and an image. As of right now, I can only post the two text variables but I would like to send an image from my UIImageView to the server by using the FILES variable from the HTTP POST Request.

Here is the working code for my POST Request:

- (IBAction)posttoserver:(id)sender {

    NSString *post = [NSString stringWithFormat:@"process=writepost&auth_token=%@&app_id=0&posttextarea=%@&url=%@", authkey, encodedmessage, encodedlink];
    NSData *data = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *postlength = [NSString stringWithFormat:@"%lu", (unsigned long)[data length]];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
    [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"my-server.com"]]];
    [request setHTTPMethod:@"POST"];
    [request setValue:postlength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:data];
    NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];

    if (connection) {
        NSLog(@"Connection!");
    }
    else {
        NSLog(@"No Connection");
    }

    [_posttext resignFirstResponder];
    [_postlink resignFirstResponder]; 

}

Continued:

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

    NSString *response = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

    NSLog(@"Post Response: %@", response);

    if ([response  isEqualToString:@"success"]) {

    // NSLog Success 

    }
    else {

    // Something is wrong

    }
}

As you can see, I am posting the user editable text as well as a link for the post. Also, you might notice that I am attaching "process=writepost&auth_token=%@&app_id=0" and those are just needed for the server to recognize who is posting and what process is being sent to the specific URL.

Now, I would like to attach an image to the POST request by adding an image from the UIImageView. How would I attach it into the FILES variable for the POST request?

Any help would be gladly appreciated.

Thanks in advance, Kyle

Rob
  • 415,655
  • 72
  • 787
  • 1,044
TechnologyGuy
  • 141
  • 3
  • 6
  • Rather than doing an `application/x-www-form-urlcoded`, you should do a `multipart/form-data` request. This is much easier if you do it via something like AFNetworking. See http://stackoverflow.com/a/24252378/1271826 for demonstration of doing it either yourself or via AFNetworking. – Rob Oct 01 '14 at 00:00
  • 1
    BTW, the `xcode` tag is intended for questions about the IDE only. As the `xcode` tag itself says, "This tag should only be used for questions about the Xcode tool itself, not for programming questions for which you happen be using Xcode". That's why rmaddy removed the tag from your question. – Rob Oct 02 '14 at 01:41

0 Answers0