Objective C’s URLRequest & URLLoader

I am learning ObjectiveC. For the model app in my head, i need the equivilant of what i understand to be a URLRequest and a URLLoader. As well, i need to understand the event callbacks(Event.COMPLETE, IOErrorEvent.IO_ERROR) and how to handle these…so, off to the Apple Dev Samples. The sample that made sense to me was the SeismicXML parser. I went straight for the  (.m file) implementation class of SeismicXMLAppDelegate . In there i found the portion of code that seems to pertain to me:


static NSString *feedURLString = @"htgggtp://earthquake.usgs.gov/eqcenter/catalogs/7day-M2.5.xml";
NSURLRequest *earthquakeURLRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:feedURLString]];
self.earthquakeFeedConnection = [[[NSURLConnection alloc] initWithRequest:earthquakeURLRequest delegate:self] autorelease];

Let’s walk through it, but first the two equivialant definitions for our URLRequst and URLLoader. In ObjectiveC , we will be working with NSURLRequest and NSURLConnection.

the NSString is the url – easy. NSURLRequest…hmmm, that sounds just like AS3’s URLRequest, doesn’t it. We declare the variable earthquakeURLRequest as a ‘new’ NSURLRequest, and we add in some methods on the fly. (i am learning that this is a key thing about ObjectiveC- it seems to do a lot in a methods in line of code.) So, we instantiate the request, as well as its requestWithURL method, passing in our url string.  In the interface (.h file), we assigned the earthquakeFeedConnection (NSURLConnection) to self (this, in as3). So, we call it to the front (self.earthquakeFeedConnection) and instantiate the class with the famously confusing alloc/init. A key feature that can not be missed is the NSURLConnection needs to konw the delegate, which is the parent class in this case (self)

ok…so, what about the call backs? Yep, just keep reading Apple’s Sample code. The first two are the recepients of a response and of data.
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
self.earthquakeData = [NSMutableData data];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[earthquakeData appendData:data];
}

then the error (like IOErrorEvent.IO_ERROR):
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
if ([error code] == kCFURLErrorNotConnectedToInternet) {
// if we can identify the error, we can present a more precise message to the user.
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:NSLocalizedString(@"No Connection Error", @"Error message displayed when not connected to the Internet.") forKey:NSLocalizedDescriptionKey];
NSError *noConnectionError = [NSError errorWithDomain:NSCocoaErrorDomain code:kCFURLErrorNotConnectedToInternet userInfo:userInfo];
[self handleError:noConnectionError];
} else {
// otherwise handle the error generically
[self handleError:error];
}
self.earthquakeFeedConnection = nil;
}

and finally, the almighty Event.COMPLETE!
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
self.earthquakeFeedConnection = nil;
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
// Spawn a thread to fetch the earthquake data so that the UI is not blocked while the application parses the XML data.
//
// IMPORTANT! - Don't access UIKit objects on secondary threads.
//
[NSThread detachNewThreadSelector:@selector(parseEarthquakeData:) toTarget:self withObject:earthquakeData];
// earthquakeData will be retained by the thread until parseEarthquakeData: has finished executing, so we no longer need
// a reference to it in the main thread.
self.earthquakeData = nil;
}

i won’t go to far into this, but there it is…all but the complete are the connection method of the NSURLConnection class. One thing to note, which i may be wrong, but appears to be the case. In ObjectiveC, the complete event seems more of a clean up. In AS3 i would generally wait for that to move forward with the data. But it appears in ObjectiveC, we start parsing/etc from didRecieveData…hmm, like i say, it’s all new to me…but i had to document this for future reference.

Tags: ,

Leave a Reply