+1  A: 

When you use NSURLConnection's initWithRequest:delegate: method, the data (along with other stuff) is sent to the delegate object in a sequence of method calls. The methods are all optional, so if your delegate object doesn't implement them, the connection object just skips them.

There are a bunch of methods, so I won't list them all here, but they're all described in detail in the NSURLConnection documentation. To get the received data you'll want to implement -connection:didReceiveData: on the delegate object. This method will be called, potentially more than once, with an NSData object representing newly-received data. You can then append that to your existing NSMutableData object, or do whatever else makes sense with it. You'll know that you've received all of the data when -connectionDidFinishLoading: is called on the delegate object.

To answer your two specific questions:

  1. Yes, you should declare it as a property of the controller object. You should also make sure to allocate the object BEFORE calling NSURLConnection's initWithRequest:delegate:, because the connection will start loading data asynchronously as soon as the connection object is created. Alternately you could implement -connection:didReceiveResponse: on the delegate, check the HTTP status, and create the data object then.

  2. The mutable data object can't find the URL, the connection, or its data as you've got it set up, but if you follow the steps I've described then you can add data to it as it comes in, and use it when the connection finishes.

Tom Harrington
A: 

Tom Harrington's got all the technical details down pat, but if you want to see what he describes working, in action, take a look at the GitHub source to either my demo/training iPhone application or ones by coders much more advanced than I am such as the Git-Phone app. Both these sample apps make the exact call you are asking about, and learning by example is often the best way.

Matthew McCullough
Thanks for your code indeed.
iPhoney