AFNetworking是一個討人喜歡的網絡庫,適用於iOS以及Mac OS X. 它構建於在NSURLConnection, NSOperation, 以及其餘熟悉的Foundation技術之上. 它擁有良好的架構,豐富的api,以及模塊化構建方式,使得使用起來很是輕鬆.例如,他可使用很輕鬆的方式從一個url來獲得json數據:html
1 | NSURL *url = [NSURL URLWithString:@"http://api.twitter.com/1/statuses/public_timeline.json"]; |
2 | NSURLRequest *request = [NSURLRequest requestWithURL:url]; |
3 | AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { |
4 | NSLog(@"Public Timeline: %@", JSON); |
5 | } failure:nil]; |
6 | [operation start]; |
AFURLConnectionOperation:一個 NSOperation 實現了NSURLConnection 的代理方法.ios
HTTP Requests:git
AFHTTPRequestOperation:AFURLConnectionOperation的子類,當request使用的協議爲HTTP和HTTPS時,它壓縮了用於決定request是否成功的狀態碼和內容類型.github
AFJSONRequestOperation:AFHTTPRequestOperation的一個子類,用於下載和處理jason response數據.面試
AFXMLRequestOperation:AFHTTPRequestOperation的一個子類,用於下載和處理xml response數據.json
AFPropertyListRequestOperation:AFHTTPRequestOperation的一個子類,用於下載和處理property list response數據.api
AFHTTPClient:捕獲一個基於http協議的網絡應用程序的公共交流模式.包含:網絡
AFImageRequestOperation:一個AFHTTPRequestOperation的子類,用於下載和處理圖片.架構
UIImageView+AFNetworking:添加一些方法到UIImageView中,爲了從一個URL中異步加載遠程圖片app
1 | NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://api.flickr.com/services/rest/?method=flickr.groups.browse&api_key=b6300e17ad3c506e706cb0072175d047&cat_id=34427469792%40N01&format=rest"]]; |
2 | AFXMLRequestOperation *operation = [AFXMLRequestOperation XMLParserRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, NSXMLParser *XMLParser) { |
3 | XMLParser.delegate = self; |
4 | [XMLParser parse]; |
5 | } failure:nil]; |
6 | [operation start]; |
1 | UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)]; |
2 | [imageView setImageWithURL:[NSURL URLWithString:@"http://i.imgur.com/r4uwx.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder-avatar"]]; |
1 | // AFGowallaAPIClient is a subclass of AFHTTPClient, which defines the base URL and default HTTP headers for NSURLRequests it creates |
2 | [[AFGowallaAPIClient sharedClient] getPath:@"/spots/9223" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { |
3 | NSLog(@"Name: %@", [responseObject valueForKeyPath:@"name"]); |
4 | NSLog(@"Address: %@", [responseObject valueForKeyPath:@"address.street_address"]); |
5 | } failure:nil]; |
01 | NSURL *url = [NSURL URLWithString:@"http://api-base-url.com"]; |
02 | AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url]; |
03 | NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"avatar.jpg"], 0.5); |
04 | NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/upload" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) { |
05 | [formData appendPartWithFileData:imageData name:@"avatar" fileName:@"avatar.jpg" mimeType:@"image/jpeg"]; |
06 | }]; |
07 | |
08 | AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] initWithRequest:request] autorelease]; |
09 | [operation setUploadProgressBlock:^(NSInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) { |
10 | NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite); |
11 | }]; |
12 | [operation start]; |
1 | NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:8080/encode"]]; |
2 | |
3 | AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] initWithRequest:request] autorelease]; |
4 | operation.inputStream = [NSInputStream inputStreamWithFileAtPath:[[NSBundle mainBundle] pathForResource:@"large-image" ofType:@"tiff"]]; |
5 | operation.outputStream = [NSOutputStream outputStreamToMemory]; |
6 | [operation start]; |