Developer Corey Johnson calls Seriously ‘the Objective-C HTTP library Apple should have created’, and it simplifies HTTP connections with the magic of blocks. In his words:
The iPhone needs a better way to make HTTP requests, specifically calls to REST web services. Seriously mixes Blocks with NSURLConnection & NSOperationQueue to do just that. It also will automatically parse the JSON response into a dictionary if the response headers are set correctly.
The following concise example makes a call to the Twitter API and returns back the resulting JSON payload already parsed into a dictionary:
NSString *url = @"http://api.twitter.com/1/users/show.json?screen_name=probablycorey;"
[Seriously get:url handler:^(id body, NSHTTPURLResposne *response, NSError *error) {
if (error) {
NSLog(@"Got error %@", error);
}
else {
NSLog(@"Look, JSON gets parsed into an dictionary");
NSLog(@"%@", [body objectForKey:@"profile_background_image_url"]);
}
}];
Here, five image requests are quickly queued up:
NSArray *urls = [NSArray arrayWithObjects:
@"http://farm5.static.flickr.com/4138/4744205956_1f08ae40e3_o.jpg,"
@"http://farm5.static.flickr.com/4123/4744238252_d11d0df5a3_b.jpg,"
@"http://farm5.static.flickr.com/4097/4743596319_50cce97d80_o.jpg,"
@"http://farm5.static.flickr.com/4099/4743581287_7c50529b36_o.jpg,"
@"http://farm5.static.flickr.com/4123/4743587437_78f0906e8a_o.jpg,"
@"http://farm5.static.flickr.com/4136/4743562971_d5f5c6d5b1_o.jpg,"
@"http://farm5.static.flickr.com/4073/4744205142_be44e64ab7_o.jpg,"
nil];
// By default the NSOperation will only do 3 requests at a time
for (NSString *url in urls) {
NSOperation *o = [Seriously request:url options:nil handler:^(id body,
NSHTTPURLResponse *response, NSError *error) {
NSLog(@"got %d (%@)", [urls indexOfObject:url], url);
}];
}
If you’re new to blocks, have a look at Apple’s own ‘A Short Guide to Blocks‘, or Joachim Bengtsson ‘Programming with C Blocks On Apple Devices‘. Over 100 APIs in iOS 4.0 use blocks already, such as Audio, Core Motion, Core Telephony and Game Kit. At a lower level, background task completion also relies on them.
Session 206 in the WWDC 2010 videos is also dedicated to ‘Introducing Blocks and Grand Central Dispatch on iPhone’.
Via Rob Righter and Ray Wenderlich on Twitter.






