0

I have string objects and image stored in nsdata. How can i send it to a server by accessing the url? I have seen examples .but its not working.Can someone tell me Thanks in Advance!!

3 Answers 3

1

You could use AFNetworking to easily send string and image to server using url.

Here is a link of tutorial how to use AFNetworking framework.

AFNetworking Tutorial

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot. With this can i send data by key/value pairs
I added new answer to show an example how to upload an image with string objects as a dictionary to a server using AFNetworking.
0

AFNetworking is the best way to do that. and there are so much tutorials out there.

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *params = @{@"username": _username.text,
                         @"password": passwordMD5
                         };
[manager POST:@"http://example.com/ws/test.php" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {

    NSLog(@"JSON: %@", responseObject); //This is the Response

    }else{

        //if you didnt get expected output then handle it here
    }




} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
     //Handle any network errors here. ex: if internet disconnected.
}];

Comments

0

Here is an example how to upload an image with string objects as a dictionary to a server using AFNetworking.

- (void)uploadPhoto {
    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://server.url"]];
    NSData *imageData = UIImageJPEGRepresentation(self.avatarView.image, 0.5);
    NSDictionary *parameters = @{@"username": self.username, @"password" : self.password};
    AFHTTPRequestOperation *op = [manager POST:@"rest.of.url" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        //do not put image inside parameters dictionary as I did, but append it!
        [formData appendPartWithFileData:imageData name:paramNameForImage fileName:@"photo.jpg" mimeType:@"image/jpeg"];
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@ ***** %@", operation.responseString, error);
    }];
    [op start];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.