AFNetworking Version | Minimum iOS Target | Minimum OS X Target | Notes |
---|---|---|---|
2.x | iOS 6 | OS X 10.8 | Xcode 5 is required. NSURLSession subspec requires iOS 7 or OS X 10.9. |
1.x | iOS 5 | Mac OS X 10.7 | |
0.10.x | iOS 4 | Mac OS X 10.6 |
使用CocoaPods進行安裝使用,Podfile以下:ios
platform :ios, '7.0' pod "AFNetworking", "~> 2.0"
1. 獲取當前設備的網絡狀態git
+ (void)netWorkStatus { /** AFNetworkReachabilityStatusUnknown = -1, // 未知 AFNetworkReachabilityStatusNotReachable = 0, // 無鏈接 AFNetworkReachabilityStatusReachableViaWWAN = 1, // 3G 花錢 AFNetworkReachabilityStatusReachableViaWiFi = 2, // WiFi */ // 若是要檢測網絡狀態的變化,必須用檢測管理器的單例的startMonitoring [[AFNetworkReachabilityManager sharedManager] startMonitoring]; // 檢測網絡鏈接的單例,網絡變化時的回調方法 [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { NSLog(@"%ld", status); }]; }
2. 獲取JSON格式數據程序員
+ (void)JSONDataWithUrl:(NSString *)url success:(void (^)(id json))success fail:(void (^)())fail; { AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; NSDictionary *dict = @{@"format": @"json"}; // 網絡訪問是異步的,回調是主線程的,所以程序員不用管在主線程更新UI的事情 [manager GET:url parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) { if (success) { success(responseObject); } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"%@", error); if (fail) { fail(); } }]; }
3. 獲取XML數據github
+ (void)XMLDataWithUrl:(NSString *)urlStr success:(void (^)(id xml))success fail:(void (^)())fail { AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; // 返回的數據格式是XML manager.responseSerializer = [AFXMLParserResponseSerializer serializer]; NSDictionary *dict = @{@"format": @"xml"}; // 網絡訪問是異步的,回調是主線程的,所以程序員不用管在主線程更新UI的事情 [manager GET:urlStr parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) { if (success) { success(responseObject); } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"%@", error); if (fail) { fail(); } }]; }
4. POST方式提交數據(其實也可以獲取數據)web
+ (void)postJSONWithUrl:(NSString *)urlStr parameters:(id)parameters success:(void (^)(id responseObject))success fail:(void (^)())fail { AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; // 設置請求格式 manager.requestSerializer = [AFJSONRequestSerializer serializer]; // 設置返回格式 manager.responseSerializer = [AFHTTPResponseSerializer serializer]; [manager POST:urlStr parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { // NSString *result = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]; if (success) { success(responseObject); } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"%@", error); if (fail) { fail(); } }]; }
5. 下載圖片json
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.scott-sherwood.com/wp-content/uploads/2013/01/scene.png"]]; AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:request imageProcessingBlock:nil success:^(NSURLRequest *request, NSHTTPURLResponse*response, UIImage *image) { self.backgroundImageView.image = image; } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) { NSLog(@"Error %@",error); }]; [operation start];
這個類庫提供一個UIImageView類別以支持加載來自網絡的遠程圖片。具備緩存管理、異步下載、同一個URL下載次數控制和優化等特徵。
使用方法.緩存
自動緩存時候緩存圖片名字是以MD5進行加密的後的名字進行命名.(由於加密那堆字串是惟一的)網絡
UITableView使用UIImageView+WebCache類(基本應用,UIImageView的一個category)dom
前提#import導入UIImageView+WebCache.h文件,而後在tableview的cellForRowAtIndexPath:方法下:異步
[imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
SDWebImageManager *manager = [SDWebImageManager sharedManager]; UIImage *cachedImage = [manager imageWithURL:url]; // 將須要緩存的圖片加載進來 if (cachedImage) { // 若是Cache命中,則直接利用緩存的圖片進行有關操做 // Use the cached image immediatly } else { // 若是Cache沒有命中,則去下載指定網絡位置的圖片,而且給出一個委託方法 // Start an async download [manager downloadWithURL:url delegate:self]; }
而後須要實現委託
// 當下載完成後,調用回調方法,使下載的圖片顯示 - (void)webImageManager:(SDWebImageManager *)imageManager didFinishWithImage:(UIImage *)image { // Do something with the downloaded image }
獨立的異步圖像緩存
SDImageCache類提供一個建立空緩存的實例,並用方法imageForKey:來尋找當前緩存。
UIImage *myCachedImage = [[SDImageCache sharedImageCache] imageFromKey:myCacheKey]; //存儲一個圖像到緩存是使用方法storeImage: forKey: [[SDImageCache sharedImageCache] storeImage:myImage forKey:myCacheKey];
默認狀況下,圖像將被存儲在內存緩存和磁盤緩存中。若是僅僅是想內存緩存中,要使用storeImage:forKey:toDisk:方法的第三個參數帶一負值來替代。