Graphics Programs Reference
In-Depth Information
The delegate of an
NSURLConnection
is responsible for overseeing the connection and
for collecting the data returned from the request. (This data is typically an XML or JSON
document; for this web service, it is XML.) However, the data returned usually comes
back in pieces, and it is the delegate's job to collect the pieces and put them together.
In
ListViewController.m
, implement
connection:didReceiveData:
to
put all of the data received by the connection into the instance variable
xmlData
.
// This method will be called several times as the data arrives
- (void)connection:(NSURLConnection *)conn didReceiveData:(NSData *)data
{
// Add the incoming chunk of data to the container we are keeping
// The data always comes in the correct order
[xmlData appendData:data];
}
When a connection has finished retrieving all of the data from a web service, it sends the
message
connectionDidFinishLoading:
to its delegate. In this method, you are
guaranteed to have the complete response from the web service request and can start
working with that data. For now, implement
connectionDidFinishLoading:
in
ListViewController.m
to print out the string representation of that data to the con-
sole to make sure good stuff is coming back.
- (void)connectionDidFinishLoading:(NSURLConnection *)conn
{
// We are just checking to make sure we are getting the XML
NSString *xmlCheck = [[NSString alloc] initWithData:xmlData
encoding:NSUTF8StringEncoding];