0

I'm having some doubts about how to upload something to a WebService.

I've been using this for getting info from my webservice:

    NSString * URLString = [NSString stringWithFormat:@"%@%@", kBaseHost, [NSString stringWithFormat:kWSProjectList, token]];
NSURL *url = [NSURL URLWithString:URLString];
NSURLRequest * request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendSynchronousRequest:request inBackgroundWithCompletionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
    NSString *json = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
    NSLog(@"%@", json);
    NSDictionary * _response = [json JSONValue];
    NSLog (@"%@", _response);
    NSNotification* notification = [NSNotification notificationWithName:kWSNotificationDidReceiveDataProjectList object:_response];
    [[NSNotificationCenter defaultCenter] postNotification:notification];
}];

Now I have to do the opposite, I have to upload the information to the WebService and I have no idea of how... Someone could guide me a little?

2
  • What is this method, sendSynchronousRequest:inBackgroundWithCompletionHandler: I don't see that one in the docs Commented Mar 30, 2012 at 18:44
  • @interface NSURLConnection (Background) + (void)sendSynchronousRequest:(NSURLRequest )request inBackgroundWithCompletionHandler:(void (^)(NSURLResponse, NSData*, NSError*))handler; Commented Apr 3, 2012 at 17:19

1 Answer 1

0

You could do it like this if you're using JSON. You'd probably want to add some error checking, but this should get you started.

-(void)sendArray {
NSArray *array = <your array here>
NSData *post = [NSJSONSerialization dataWithJSONObject:array options:NSJSONWritingPrettyPrinted error:nil];
NSString *postLength = [NSString stringWithFormat:@"%d", [post length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:kServerPath]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:post];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setTimeoutInterval:5];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse* theResponse, NSData* theData, NSError* error){
    //Do whatever with return data
}];

}

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

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.