AFNetwork是一個(gè)輕量級(jí)的網(wǎng)絡(luò)請(qǐng)求api類庫(kù)。是以NSURLConnection, NSOperation和其他方法為基礎(chǔ)的。
Github:https://github.com/AFNetworking/AFNetworking/
下面這個(gè)例子是用來(lái)處理json請(qǐng)求的:
NSURL *url = [NSURL URLWithString:@"https://alpha-api.app.net/stream/0/posts/stream/global"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(@"App.net Global Stream: %@", JSON);
} failure:nil];
[operation start];
使用方法
1.下載AFNetwork,點(diǎn)擊下載
2.將文件夾名稱為AFNetworking拖入到你的工程項(xiàng)目即可
常見問(wèn)題
AFNetworking作用都有哪些?
NSURLConnection提供了+sendAsynchronousRequest:queue:completionHandler:和+sendAsynchronousRequest:queue:completionHandler: ,但是AFNetworking提供了更好的功能
*AFURLConnectionOperation和它的子類繼承NSOperation的,允許請(qǐng)求被取消,暫停/恢復(fù)和由NSOperationQueue進(jìn)行管理。
*AFURLConnectionOperation也可以讓你輕松得完成上傳和下載,處理驗(yàn)證,監(jiān)控上傳和下載進(jìn)度,控制的緩存。
*AFHTTPRequestOperation和它得子類可以基于http狀態(tài)和內(nèi)容列下來(lái)區(qū)分是否成功請(qǐng)求了
*AFNetworking可以將遠(yuǎn)程媒體數(shù)據(jù)類型(NSData)轉(zhuǎn)化為可用的格式,比如如JSON,XML,圖像和plist。
*AFHTTPClient提供了一個(gè)方便的網(wǎng)絡(luò)交互接口,包括默認(rèn)頭,身份驗(yàn)證,是否連接到網(wǎng)絡(luò),批量處理操作,查詢字符串參數(shù)序列化,已經(jīng)多種表單請(qǐng)求
*UIImageView+ AFNetworking增加了一個(gè)方便的方法來(lái)異步加載圖像。AFNetworking是否支持緩存?
可以,NSURLCache及其子類提供了很多高級(jí)接口用于處理緩存
如果你想將緩存存儲(chǔ)再磁盤,推薦使用SDURLCache如何使用AFNetworking上傳一個(gè)文件?
NSData *imageData = UIImagePNGRepresentation(image);
NSURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:@"/upload" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData> formData) {
[formData appendPartWithFileData:imageData mimeType:@"image/png" name:@"avatar"];
}];
4 .如何使用AFNetworking下載一個(gè)文件?
先創(chuàng)建一個(gè)AFURLConnectionOperation對(duì)象,然后再使用它的屬性outputStream進(jìn)行處理
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:@"download.zip" append:NO];
- 如何解決:SystemConfiguration framework not found in project
請(qǐng)導(dǎo)入:
#import <SystemConfiguration/SystemConfiguration.h>
#import <MobileCoreServices/MobileCoreServices.h>
- 當(dāng)應(yīng)用程序退出時(shí),如何保持持續(xù)的請(qǐng)求?
AFURLConnectionOperation有一個(gè)叫setShouldExecuteAsBackgroundTaskWithExpirationHandler:的方法用于處理在應(yīng)用程序進(jìn)入后臺(tái)后,進(jìn)行持續(xù)的請(qǐng)求
[self setShouldExecuteAsBackgroundTaskWithExpirationHandler:^{
}];
一些實(shí)例
- XML 請(qǐng)求
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"]];
AFXMLRequestOperation *operation = [AFXMLRequestOperation XMLParserRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, NSXMLParser *XMLParser) {
XMLParser.delegate = self;
[XMLParser parse];
} failure:nil];
[operation start];
- 圖片請(qǐng)求:
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"]];
- 圖片上傳處理,監(jiān)測(cè)上傳狀態(tài):
NSURL *url = [NSURL URLWithString:@"http://api-base-url.com"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"avatar.jpg"], 0.5);
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/upload" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
[formData appendPartWithFileData:imageData name:@"avatar" fileName:@"avatar.jpg" mimeType:@"image/jpeg"];
}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
}];
[operation start];
4.在線流媒體請(qǐng)求
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:8080/encode"]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.inputStream = [NSInputStream inputStreamWithFileAtPath:[[NSBundle mainBundle] pathForResource:@"large-image" ofType:@"tiff"]];
operation.outputStream = [NSOutputStream outputStreamToMemory];
[operation start];