-1

It,s my first time trying to send Images & Data to a PHP Server from an app, and I am quite confused as to what's the best way to achieve this.

In my research, I came across AFNetworking library and this base64 library (which I would take suggestion in another one), but I don't know if I can achieve what I want with that or how to implement it.

What I want to do is to send data & images that have a relationship.

Lets say the User has to upload their user details + their picture and their house details + a picture my JSON would be something like

{
    "userDetails": { "name":"jon",
                  "surname":"smith",
                  "phone":"123412",
                  "userPic":"base64pic"
                },
    "house": { "address":"123 asd",
               "postcode":"w2 e23",
               "housePic":"base64pic"
            }

}

of course that JSON would also have to include security validations.

My problem comes when I would like to avoid using base64 encoding given the 33% size increase and I don't know how I could send the same information to PHP.

I get very confused when trying to send images & data that have a relationship and should be stored taking that relationship into account in the server.

Basically What I am looking for is a way to send the same information but not base64 encoded images but keeping the relationship in the data and trying to send as fewer request as possible. Is it possible? if so How?

Undo
  • 25,519
  • 37
  • 106
  • 129
Jonathan Thurft
  • 4,087
  • 7
  • 47
  • 78

1 Answers1

0

look at this example for instance, eveything is pretty self explanatory but ask me if you have any questions

-(void) postStuff{
        AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"https://www.yourdomain.com/"]];
        NSDictionary *parameter = @{@"body"@"Anything you want to say!"};
        NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"api/v1/posts/newpost/" parameters:parameter constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
            [formData appendPartWithFileData:imageData name:@"image" fileName:@"image.png" mimeType:@"image/png"];
        }];
        AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
        [operation  setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSLOG(@"DONE!!!");
        }
        failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLOG(@"Error: %@", error);
        }];
        [httpClient enqueueHTTPRequestOperation:operation];
}
Jonathan
  • 2,728
  • 10
  • 43
  • 73