Updating README to match 0.3.0 changes
This commit is contained in:
parent
3f901c81b3
commit
ba36241932
1 changed files with 58 additions and 48 deletions
102
README.md
102
README.md
|
|
@ -1,75 +1,85 @@
|
||||||
# AFNetworking
|
# AFNetworking
|
||||||
## A delightful iOS networking library with NSOperations and block-based callbacks
|
## A delightful iOS networking library with NSOperations and block-based callbacks
|
||||||
|
### 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).
|
||||||
|
|
||||||
**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).**
|
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, `NSCache` and `NSURLCache` for performant and compliant cacheing behavior, and blocks to keep HTTP request / response handling code in a single logical unit in code.
|
||||||
|
|
||||||
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.
|
|
||||||
|
|
||||||
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.
|
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.
|
||||||
|
|
||||||
## Example Usage
|
## Example Usage
|
||||||
|
|
||||||
### GET Request
|
### JSON Request
|
||||||
|
|
||||||
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://gowalla.com/users/mattt.json"]];
|
``` objective-c
|
||||||
AFCallback *callback = [AFHTTPOperationCallback callbackWithSuccess:^(NSURLRequest *request, NSHTTPURLResponse *response, NSDictionary *data) {
|
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://gowalla.com/users/mattt.json"]];
|
||||||
NSLog(@"Name: %@ %@", [data valueForKeyPath:@"first_name"], [data valueForKeyPath:@"last_name"]);
|
AFJSONRequestOperation *operation = [AFJSONRequestOperation operationWithRequest:request success:^(id JSON) {
|
||||||
}];
|
NSLog(@"Name: %@ %@", [JSON valueForKeyPath:@"first_name"], [JSON valueForKeyPath:@"last_name"]);
|
||||||
[[AFHTTPOperation operationWithRequest:request callback:callback] start];
|
}];
|
||||||
|
[operation start];
|
||||||
### POST Request With HTTP Authorization Header Using `NSOperationQueue`
|
```
|
||||||
|
|
||||||
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://gowalla.com/friendships/request?user_id=1699"]];
|
|
||||||
[request setHTTPMethod:@"POST"];
|
|
||||||
|
|
||||||
NSDictionary *headers = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Token token=\"%@\"", kOAuthToken] forKey:@"Authorization"];
|
|
||||||
[request setAllHTTPHeaderFields:headers];
|
|
||||||
|
|
||||||
AFCallback *callback = [AFHTTPOperationCallback callbackWithSuccess:^(NSURLRequest *request, NSHTTPURLResponse *response, NSDictionary *data) {
|
|
||||||
NSLog(@"Friend Request Sent");;
|
|
||||||
} error:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
|
|
||||||
NSLog(@"[Error] (%@ %@) %@", [request HTTPMethod], [[request URL] relativePath], error);
|
|
||||||
}];
|
|
||||||
|
|
||||||
AFHTTPOperation *operation = [AFHTTPOperation operationWithRequest:request callback:callback];
|
|
||||||
[[NSOperationQueue mainQueue] addOperation:operation];
|
|
||||||
|
|
||||||
### Image Request
|
### Image Request
|
||||||
|
|
||||||
NSURL *imageURL = [NSURL URLWithString:@"http://s3.amazonaws.com/static.gowalla.com/users/1699-standard.jpg"];
|
``` objective-c
|
||||||
NSURLRequest *request = [NSURLRequest requestWithURL:imageURL];
|
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)];
|
||||||
|
[imageView setImageWithURL:[NSURL URLWithString:@"http://i.imgur.com/r4uwx.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder-avatar"]];
|
||||||
|
```
|
||||||
|
|
||||||
AFCallback *callback = [AFImageRequestOperationCallback callbackWithSuccess:^(UIImage *image) {
|
### HTTP POST Request With Authorization Header Using `NSOperationQueue`
|
||||||
self.imageView.image = image;
|
|
||||||
} imageSize:CGSizeMake(50.0f, 50.0f) options:AFImageRequestResize | AFImageRequestRoundCorners];
|
|
||||||
[[AFImageRequestOperation operationWithRequest:request callback:callback] start];
|
|
||||||
|
|
||||||
### REST Client Request
|
``` objective-c
|
||||||
|
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://gowalla.com/"]];
|
||||||
|
AFHTTPRequestOperation *operation = [AFHTTPRequestOperation operationWithRequest:request completion:^(NSURLRequest *request, NSHTTPURLResponse *response, NSData *data, NSError *error) {
|
||||||
|
NSLog(@"HTTP Status Code: %d", [response statusCode]);
|
||||||
|
}];
|
||||||
|
[operation start];
|
||||||
|
```
|
||||||
|
|
||||||
// AFGowallaAPIClient is a subclass of AFRestClient, which defines the base URL and default HTTP headers of NSURLRequests it creates
|
### POST Request With HTTP Authorization Header Using `NSOperationQueue`
|
||||||
[[AFGowallaAPIClient sharedClient] getPath:@"/spots/9223" parameters:nil callback:[AFHTTPOperationCallback callbackWithSuccess:^(NSURLRequest *request, NSHTTPURLResponse *response, NSDictionary *data) {
|
|
||||||
NSLog(@"%@: %@", [data valueForKeyPath:@"name"], [data valueForKeyPath:@"address.street_address"]);
|
``` objective-c
|
||||||
}]];
|
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://gowalla.com/friendships/request?user_id=1699"]];
|
||||||
|
[request setHTTPMethod:@"POST"];
|
||||||
|
|
||||||
|
NSDictionary *headers = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Token token=\"%@\"", kOAuthToken] forKey:@"Authorization"];
|
||||||
|
[request setAllHTTPHeaderFields:headers];
|
||||||
|
|
||||||
|
AFHTTPRequestOperation *operation = [AFHTTPRequestOperation operationWithRequest:request completion:^(NSURLRequest *request, NSHTTPURLResponse *response, NSData *data, NSError *error) {
|
||||||
|
BOOL HTTPStatusCodeIsAcceptable = [[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(200, 100)] containsIndex:[response statusCode]];
|
||||||
|
if (HTTPStatusCodeIsAcceptable) {
|
||||||
|
NSLog(@"Friend Request Sent");
|
||||||
|
} else {
|
||||||
|
NSLog(@"[Error] (%@ %@) %@", [request HTTPMethod], [[request URL] relativePath], error);
|
||||||
|
}
|
||||||
|
}];
|
||||||
|
|
||||||
|
[[NSOperationQueue mainQueue] addOperation:operation];
|
||||||
|
```
|
||||||
|
|
||||||
|
### REST API Client Request
|
||||||
|
|
||||||
|
``` objective-c
|
||||||
|
// AFGowallaAPIClient is a subclass of AFRestClient, which defines the base URL and default HTTP headers for NSURLRequests it creates
|
||||||
|
[[AFGowallaAPIClient sharedClient] getPath:@"/spots/9223" parameters:nil success:^(id response) {
|
||||||
|
NSLog(@"Name: %@", [response valueForKeyPath:@"name"]);
|
||||||
|
NSLog(@"Address: %@", [response valueForKeyPath:@"address.street_address"]);
|
||||||
|
}];
|
||||||
|
```
|
||||||
|
|
||||||
## Example Project
|
## Example Project
|
||||||
|
|
||||||
In order to demonstrate the power and flexibility of AFNetworking, we've included a small sample project, which 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.
|
In order to demonstrate the power and flexibility of AFNetworking, we've included a small sample project, which asks for your current location and displays [Gowalla](http://gowalla.com/) spots nearby you. It uses `AFJSONRequestOperation` to load and parse the spots JSON, and a category on `UIImageView` to asynchronously load spot stamp images as you scroll.
|
||||||
|
|
||||||
Take a close look at `AFGowallaAPIClient` 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`.
|
|
||||||
|
|
||||||
## Dependencies
|
## Dependencies
|
||||||
|
|
||||||
* [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.
|
* [iOS 4.0+](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.
|
||||||
* [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.
|
* [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. (`NSJSONSerialization`, introduced in iOS 5 is used if available, and falls back on JSONKit)
|
||||||
* [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.
|
|
||||||
|
|
||||||
## Credits
|
## Credits
|
||||||
|
|
||||||
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).
|
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).
|
||||||
|
|
||||||
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.
|
[TTTLocationFormatter](), used in the example project, is part of [FormatterKit](https://github.com/mattt/FormatterKit), created by [Mattt Thompson](https://github.com/mattt/).
|
||||||
|
|
||||||
[TTTLocationFormatter](), used in the example project, was created by [Mattt Thompson](https://github.com/mattt/TTTLocationFormatter/).
|
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue