Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 88 lines (60 sloc) 6.609 kb
87a90fe @mattt Adding README
mattt authored
1 # AFNetworking
2 ## A delightful iOS networking library with NSOperations and block-based callbacks
3
4 **There's a lot to be said for a networking library that you can wrap your head around. API design matters, too--code at its best is poetry, and should be designed to delight (but never surprise).**
5
6 AFNetworking was lovingly crafted to make best use of our favorite parts of Apple's `Foundation` framework: `NSOperation` for managing multiple concurrent requests, `NSURLRequest` & `NSHTTPURLResponse` to encapsulate state, `NSURLCache` for performant and compliant cacheing behavior, and blocks to keep HTTP request / response handling code in a single logical unit in code.
7
8 If you're tired of massive libraries that try to do too much, if you've taken it upon yourself to roll your own hacky solution, if you want a library that _actually makes iOS networking code kinda fun_, try out AFNetworking.
9
10 ## Example Usage
11 ### GET Request
12
13 NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://gowalla.com/users/mattt.json"]];
14 AFCallback *callback = [AFHTTPOperationCallback callbackWithSuccess:^(NSURLRequest *request, NSHTTPURLResponse *response, NSDictionary *data) {
15 NSLog(@"Name: %@ %@", [data valueForKeyPath:@"first_name"], [data valueForKeyPath:@"last_name"]);
16 }];
17 [[AFHTTPOperation operationWithRequest:request callback:callback] start];
18
19 ### POST Request With HTTP Authorization Header Using `NSOperationQueue`
20
21 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://gowalla.com/friendships/request?user_id=1699"]];
22 [request setHTTPMethod:@"POST"];
23
24 NSDictionary *headers = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Token token=\"%@\"", kOAuthToken] forKey:@"Authorization"];
25 [request setAllHTTPHeaderFields:headers];
26
27 AFCallback *callback = [AFHTTPOperationCallback callbackWithSuccess:^(NSURLRequest *request, NSHTTPURLResponse *response, NSDictionary *data) {
28 NSLog(@"Friend Request Sent");;
29 } error:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
30 NSLog(@"[Error] (%@ %@) %@", [request HTTPMethod], [[request URL] relativePath], error);
31 }];
32
33 AFHTTPOperation *operation = [AFHTTPOperation operationWithRequest:request callback:callback];
34 [[NSOperationQueue mainQueue] addOperation:operation];
35
36 ### Image Request
37
38 NSURL *imageURL = [NSURL URLWithString:@"http://s3.amazonaws.com/static.gowalla.com/users/1699-standard.jpg"];
39 NSURLRequest *request = [NSURLRequest requestWithURL:imageURL];
40
41 AFCallback *callback = [AFImageRequestOperationCallback callbackWithSuccess:^(UIImage *image) {
42 self.imageView.image = image;
43 } imageSize:CGSizeMake(50.0f, 50.0f) options:AFImageRequestResize | AFImageRequestRoundCorners];
44 [[AFImageRequestOperation operationWithRequest:request callback:callback] start];
45
46 ## Example Project
47
48 In order to demonstrate the power and flexibility of AFNetworking, we've included a small sample project. `AFNetworkingExample` asks for your current location and displays [Gowalla](http://gowalla.com/) spots nearby you. It uses `AFHTTPOperation` to load and parse the spots JSON, and `AFImageRequestOperation` to asynchronously load spot stamp images as you scroll.
49
50 Take a close look at `AFGowallaAPI` and `AFImageRequest`. These two classes provide convenience methods on top of the core AFNetworking classes. They provide higher-level methods for creating requests, and enqueueing them into an `NSOperationQueue`. In the case of `AFGowallaAPI`, acts as an REST client that manages HTTP headers and encodes `NSDictionary` parameters into the `GET` URL query string or `POST/PUT/DELETE` request body. You can follow this design when creating your own REST client for a particular webservice.
51
52 ## Dependencies
53
54 * [iOS 4](http://developer.apple.com/library/ios/#releasenotes/General/WhatsNewIniPhoneOS/Articles/iPhoneOS4.html%23//apple_ref/doc/uid/TP40009559-SW1) - `AFNetworking` uses blocks, which were introduced in iOS 4.
55 * [QHTTPOperation](http://developer.apple.com/library/ios/#samplecode/MVCNetworking/Listings/Networking_QHTTPOperation_m.html) - Underneath `AFHTTPOperation` and `AFImageRequestOperation` is `QHTTPOperation`, an `NSOperation` subclass that manages `NSURLConnection` delegate methods. It's fairly robust, performant, and complete, so rather than roll our own, we built `AFNetworking` on top of this. We may build our own replacement if the need arises.
56 * [JSONKit](https://github.com/johnezang/JSONKit) - One of the conveniences built into `AFHTTPOperation` is automatic JSON parsing for HTTP requests that return content-type `application/json`. JSONKit is our preferred JSON parsing library, and is included in the example project.
57
58 ## Credits
59
60 AFNetworking was created by [Scott Raymond](https://github.com/sco/) and [Mattt Thompson](https://github.com/mattt/) in the development of [Gowalla for iPhone](http://itunes.apple.com/us/app/gowalla/id304510106?mt=8).
61
62 QRunLoopOperation and QHTTPOperation were created by Apple DTS Engineers as a part of the sample code project [MVC Networking](http://developer.apple.com/library/ios/#samplecode/MVCNetworking/Introduction/Intro.html). See corresponding files for copyright and usage information.
63
64 [TTTLocationFormatter], used in the example project, was created by [Mattt Thompson](https://github.com/mattt/).
65
66 ## License
67
68 AFNetworking is licensed under the MIT License:
69
70 Copyright (c) 2011 Gowalla (http://gowalla.com/)
71
72 Permission is hereby granted, free of charge, to any person obtaining a copy
73 of this software and associated documentation files (the "Software"), to deal
74 in the Software without restriction, including without limitation the rights
75 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
76 copies of the Software, and to permit persons to whom the Software is
77 furnished to do so, subject to the following conditions:
78
79 The above copyright notice and this permission notice shall be included in
80 all copies or substantial portions of the Software.
81
82 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
83 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
84 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
85 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
86 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
87 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
88 THE SOFTWARE.
Something went wrong with that request. Please try again.