1

I am currently using the following code to attempt at a NSMutableURLRequest.

NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:allStrings]];
[postRequest setHTTPMethod:@"POST"];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:postRequest delegate:self];

I do not know how to actually initiate the post. What actually fires it off at the URL? I have everything setup but *conn is not being used yet, so how do I use *conn?

daemon
  • 251
  • 3
  • 21

3 Answers3

1

Creating the NSURLConnection will fire the connection.

Now you have to implement the callbacks, esp. connectionDidFinishLoading to get the results.

Here are the methods for the NSURLConnectionDataDelegate. From the NSURLConnection initWithRequest:delegate: docs:

Discussion
This is equivalent to calling initWithRequest:delegate:startImmediately: and passing YES for startImmediately.

Mundi
  • 79,884
  • 17
  • 117
  • 140
0
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
M.B
  • 885
  • 2
  • 15
  • 31
0

You have many way to start an NSURLConnection.

[NSURLConnection initWithRequest:postRequest delegate:self];

Returns an initialized URL connection and begins to load the data for the URL request.

So it's weird to have an unused variable you can start it manually :

NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:postRequest delegate:self startImmediatly:NO];
[conn start];

Or not get the result

[[NSURLConnection alloc] initWithRequest:postRequest delegate:self];

Or even better to retain the NSURLConnection, store it in a property :

self.URLConnection = [[NSURLConnection alloc] initWithRequest:postRequest delegate:self];

Note there are now two convenient helpers methods :

[NSURLConnection sendSynchronousRequest:returningResponse:error:];

Which returns a NSData

or

[NSURLConnection sendAsynchronousRequest:queue:completionHandler:];

You can directly use block as a callback.

Francescu
  • 16,974
  • 6
  • 49
  • 60