afnetworking
DESCRIPTION
AFNetworking slides from my presentation at the October 2011 meeting of the Houston iPhone Developers Meetup.TRANSCRIPT
AFNetworking
Houston iPhone Developers MeetupOctober 25th, 2011
•Full time contract iOS developer out of The Woodlands
•Contact me at http://ipanemalabs.com
•Founder of Houston iPhone Developers Meetup
•Organizer of Houston PHP Users Group
•Personal iPhone apps on store since 2008
•Personal site at http://pessoal.org
2
AFNetworking
Who?
•Overview of what AFNetworking provides
•Background on the project
•Alternatives to it
•Bunch of code examples
•Feel free to ask questions
3
AFNetworking
Tonight’s session
•Open-source library shared by Gowalla
•Available at https://github.com/gowalla/AFNetworking
•Created in the development of Gowalla for iPhone
•Inspired by an Apple sample project (MVC Networking)
•Uses Objective-C blocks for everything (requires iOS 4+)
•MIT license
4
AFNetworking
Background
•Main authors very active behind the project
•Mattt Thompson - http://mattt.me
•Scott Raymond - http://scottraymond.net
5
AFNetworking
More background
•ASIHTTPRequest
•Convenience wrapper with lots of useful features
•Author decided to stop maintaining it
•NSURLConnection
•Lots of tedious / boilerplate code to manage HTTP requests
•Other options
•RestKit - http://restkit.org
•SVHTTPRequest - http://github.com/samvermette/SVHTTPRequest
6
AFNetworking
Alternatives
•Uses NSOperation for scheduling and concurrency
•Blocks for request completion / failure callbacks
•Convenience features:
•Requests for JSON, XML, Image data, etc
•File upload with progress callback
•API client request
7
AFNetworking
AFNetworking features
•Clone AFNetworking github project
•Copy “AFNetworking” sub-folder to your own Xcode project
•Clone JSONKit github project
•https://github.com/johnezang/JSONKit
•Copy JSONKit.h and JSONKit.m to your own Xcode project
8
AFNetworking
Installation
•Provides appledoc compatible documentation
•Appledoc available at https://github.com/tomaz/appledoc
•git clone https://github.com/tomaz/appledoc.git
•sudo sh install-appledoc.sh
•Installs on /usr/local/bin
•git clone https://github.com/gowalla/AFNetworking.git
•cd AFNetworking
•appledoc -p AFNetworking -c "Gowalla" --company-id com.gowalla
AFNetworking/*.h
9
AFNetworking
Great documentation
10
AFNetworking
Documentation
•Closures for Objective-C
•Similar in nature to JavaScript closures, but slightly different
•Great for callback-centric code
•Available since iOS 4
•Used a lot in Grand Central Dispatch (GCD)
•Great introduction documentation available from Apple
•http://developer.apple.com/library/ios/#documentation/cocoa/
Conceptual/Blocks/Articles/00_Introduction.html
11
AFNetworking
Blocks
12
AFNetworking
Blocks
•Library core is AFURLConnectionOperation
•NSOperation subclass to make asynchronous requests
•Subclasses
•AFJSONRequestOperation – JSON requests
•AFXMLRequestOperation – XML requests
•AFPropertyListRequestOperation – Plist requests
•AFImageRequestOperation – Image requests
13
AFNetworking
Core classes
14
AFNetworking
JSON request
•Dependency on JSONKit
•Very high performance JSON library
•Benchmark results available at https://github.com/johnezang/JSONKit
•Claimed to be faster than parsing/serializing binary plists.
•Results contested by TouchJSON author
15
AFNetworking
JSON request
•Similar to JSON request, but no automatic parsing of payload
•Block callback allows you to kick off the XML parsing
16
AFNetworking
XML request
17
AFNetworking
XML request
•Load remote images on UIImageView asynchronously
•Alternative to EGOImageView and similar libraries
•Great on table view cells, async requests get canceled as cells are re-used
•Simple interface to load images
•-[UIImageView setImageWithURL:placeholderImage:]
•More control available through a different method
18
AFNetworking
Image request
19
AFNetworking
Image request
•AFHTTPClient at the core
•Detects appropriate operation for each request
•AFGowallaAPIClient subclass of that
•Easy to extend core class with the business logic for your own web service
implementation
20
AFNetworking
API request
21
AFNetworking
AFGowallaAPIClient
•AFNetworking post on Gowalla’s Engineering blog
•http://engineering.gowalla.com/2011/10/24/afnetworking/
•Github pages
•Great information on issue list, network graph
•Mattt’s profile page - https://github.com/mattt/
•TTTAttributedLabel - https://github.com/mattt/TTTAttributedLabel
•FormatterKit - https://github.com/mattt/FormatterKit
22
AFNetworking
More information
Contact: