It's difficult to find any app that doesn't connect to the network to get data. If you have used NSURLConnection you know that fetching data is easy, but can be fraught with a messy implementation. AFNetworking is delightful networking library for iOS and Mac that can simplify the process of getting JSON data, XML, or even images.
4. NSData
• One line of code!
!
dataWithContentsOfURL:
!
NEVER USE THIS API!
5. CFNetwork NSURL *url = [NSURL URLWithString:DOWNLOAD_URL];
!
CFHTTPMessageRef request = CFHTTPMessageCreateRequest(NULL, CFSTR("GET"), (__bridge CFURLRef)url, kCFHTTPVersion1_1);
!
CFReadStreamRef requestStream = CFReadStreamCreateForHTTPRequest(NULL, request);
CFReadStreamOpen(requestStream);
NSMutableData *responseBytes = [NSMutableData data];
CFIndex numBytesRead = 0 ;
NSUInteger totalBytesRead = 0;
!
do {
UInt8 buf[1024];
numBytesRead = CFReadStreamRead(requestStream, buf, sizeof(buf));
if(numBytesRead > 0) {
[responseBytes appendBytes:buf length:numBytesRead];
totalBytesRead += numBytesRead;
}
} while(numBytesRead > 0);
if (totalBytesRead > 0) {
CFHTTPMessageRef response = (CFHTTPMessageRef)CFReadStreamCopyProperty(requestStream, kCFStreamPropertyHTTPResponseHeader);
CFHTTPMessageSetBody(response, (__bridge CFDataRef)responseBytes);
CFReadStreamClose(requestStream);
CFDataRef responseBodyData = CFHTTPMessageCopyBody(response);
NSError *error = nil;
NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:(__bridge NSData *)responseBodyData options:0 error:&error];
CFRelease(responseBodyData);
CFRelease(response);
if (!jsonData) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"The data was corrupt. Sorry"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
} else {
NSString *ip = jsonData[@"ip"];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success!"
message:[NSString stringWithFormat:@"Your IP is: %@", ip]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"There was an error getting the data. Please try again later."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
!
CFRelease(requestStream);
CFRelease(request);
• All this code, just to download
and display this:
{"ip": "72.191.49.142"}
• Plus we have to write gross C
with nasty CFRelease()
6. AFNetworking
“AFNetworking is a delightful networking library for iOS and Mac OS
X. It's built on top of the Foundation URL Loading System, extending
the powerful high-level networking abstractions built into Cocoa. It
has a modular architecture with well-designed, feature-rich APIs that
are a joy to use.”
12. Key Concepts
• AF*Operation are subclasses of NSOperation
• Networking operations can be started individually or placed in an
NSOperationQueue
• Custom serialization classes can be created for transforming and
validating data.
14. AFURLResponseSerializer
• If you don’t specify a request/response serializer, it will give back
NSData.
• Serializers can be created to handle data transformation and
validation.
21. AFNetworking + UIKit
• AFNetworking adds a number of extensions to common UIKit
classes:
• UIImageView
• UIButton
• UIWebView
• UIProgressView
• UIRefreshControl
29. Monster View Controller
• UITableView Datasource
• Configure Cells
• UITableView Delegate
• Showing Member Details
• NSFetchedResultsControllerDelegate
• Fetch All Member data
• Fetch Checked-In Members
• Fetch Profile Images
• Search Members By Name
• Search Members By Skills
• Data Persistance
• Check-In logic
• Checked-In vs All Members