1.NSURLSession概述
1. NSURLSession session類型
NSURLSession包括下面3種session類型
a). Default session(默認會話模式):
使用的是基于磁盤緩存的持久化策略,工作模式類似于原來的NSURLConnection,可以用來取代NSURLConnection中的:
[NSURLConnection sendAsynchronousRequest:queue:completionHandler:]
b). Ephemeral session(瞬時會話模式):
臨時的進程內會話(內存),不會將cookie、證書、緩存儲存到本地,只會放到內存中,當應用程序退出后數據也會消失。
c). Background session(后臺會話模式):
和默認會話模式類似, 不過相比默認模式,該會話會在后臺開啟一個線程進行網絡數據處理。
//Default session
+ (NSURLSessionConfiguration *)defaultSessionConfiguration;
//Ephemeral
+ (NSURLSessionConfiguration *)ephemeralSessionConfiguration;
//Background
+ (NSURLSessionConfiguration *)backgroundSessionConfiguration:(NSString *)identifier;
你需要了解包括NSURLSession、NSURLSessionConfiguration 以及 NSURLSessionTask 的 3 個子類:NSURLSessionDataTask,NSURLSessionUploadTask,NSURLSessionDownloadTask。
與 NSURLConnection 相比,NSURLsession 最直接的改進就是可以配置每個 session 的緩存,協議,cookie,以及證書策略,甚至跨程序共享這些信息。這將允許程序和網絡基礎框架之間相互獨立,不會發生干擾。每個 NSURLSession 對象都由一個 NSURLSessionConfiguration 對象來進行初始化。
NSURLSession 中另一大塊就是 session task。它負責處理數據的加載以及文件和數據在客戶端與服務端之間的上傳和下載。NSURLSessionTask 與 NSURLConnection 最大的相似之處在于它也負責數據的加載,最大的不同之處在于所有的 task 共享其創造者 NSURLSession 這一公共委托者(common delegate)。
2.NSURLSessionTask類
NSURLSessionTask是一個抽象子類,它有三個子類:NSURLSessionDataTask,NSURLSessionUploadTask和NSURLSessionDownloadTask。這三個類封裝了現代應用程序的三個基本網絡任務:獲取數據,比如JSON或XML,以及上傳和下載文件。
下面是其繼承關系:
不同于直接使用 alloc-init 初始化方法,task 是由一個 NSURLSession 創建的。每個 task 的構造方法都對應有或者沒有 completionHandler 這個 block 的兩個版本。
1).NSURLSessionDataTask
NSURLSessionDataTask使用NSData來交換數據. NSURLSessionDataTask不支持后臺會話模式。
a) 通過request對象或url創建
//通過一個給定的請求
- (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request;
//通過一個給定的URL.
- (NSURLSessionDataTask *)dataTaskWithURL:(NSURL *)url;
代理方法:
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
didReceiveResponse:(NSURLResponse *)response
completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler
{
completionHandler(NSURLSessionResponseAllow);
}
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
didReceiveData:(NSData *)data
{
//data: response from the server.
}
b).通過request對象或url創建,同時指定任務完成后通過completionHandler指定回調的代碼塊:
- (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request completionHandler:(void (^)(NSData *data, NSURLResponse *response, NSError *error))completionHandler;
- (NSURLSessionDataTask *)dataTaskWithURL:(NSURL *)url completionHandler:(void (^)(NSData *data, NSURLResponse *response, NSError *error))completionHandler;
2).NSURLSessionDownloadTask
a).通過URL/request/resumeData創建
- (NSURLSessionDownloadTask *)downloadTaskWithRequest:(NSURLRequest *)request;
- (NSURLSessionDownloadTask *)downloadTaskWithURL:(NSURL *)url;
//下載任務支持斷點續傳,這種方式是通過之前已經下載的數據來創建下載任務。
- (NSURLSessionDownloadTask *)downloadTaskWithResumeData:(NSData *)resumeData;
代理方法:
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
didFinishDownloadingToURL:(NSURL *)location;
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
didWriteData:(int64_t)bytesWritten
totalBytesWritten:(int64_t)totalBytesWritten
totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite;
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
didResumeAtOffset:(int64_t)fileOffset
expectedTotalBytes:(int64_t)expectedTotalBytes;
b).通過completionHandler指定任務完成后的回調代碼塊:
- (NSURLSessionDownloadTask *)downloadTaskWithRequest:(NSURLRequest *)request completionHandler:(void (^)(NSURL *location, NSURLResponse *response, NSError *error))completionHandler;
- (NSURLSessionDownloadTask *)downloadTaskWithURL:(NSURL *)url completionHandler:(void (^)(NSURL *location, NSURLResponse *response, NSError *error))completionHandler;
- (NSURLSessionDownloadTask *)downloadTaskWithResumeData:(NSData *)resumeData completionHandler:(void (^)(NSURL *location, NSURLResponse *response, NSError *error))completionHandler;
3).NSURLSessionUploadTask
a).通過request創建,在上傳時指定文件源或數據源。
- (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request fromFile:(NSURL *)fileURL;
- (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request fromData:(NSData *)bodyData;
- (NSURLSessionUploadTask *)uploadTaskWithStreamedRequest:(NSURLRequest *)request;
代理方法:
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
didSendBodyData:(int64_t)bytesSent
totalBytesSent:(int64_t)totalBytesSent
totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend;
b).通過completionHandler指定任務完成后的回調代碼塊
- (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request fromFile:(NSURL *)fileURL completionHandler:(void (^)(NSData *data, NSURLResponse *response, NSError *error))completionHandler;
- (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request fromData:(NSData *)bodyData completionHandler:(void (^)(NSData *data, NSURLResponse *response, NSError *error))completionHandler;
3.NSURLSession類
從上面我們已經知道創建一個NSURLSessionTask需要NSURLSession,獲取NSURLSession類對象有幾種方式:
//使用全局的Cache,Cookie和證書。
+ (NSURLSession *)sharedSession;
//創建對應配置的會話。
+ (NSURLSession *)sessionWithConfiguration:(NSURLSessionConfiguration *)configuration;
+ (NSURLSession *)sessionWithConfiguration:(NSURLSessionConfiguration *)configuration delegate:(id <NSURLSessionDelegate>)delegate delegateQueue:(NSOperationQueue *)queue;
第三種方式指定了session的委托和委托所處的隊列。當不再需要連接時,可以調用Session的invalidateAndCancel直接關閉,或者調用finishTasksAndInvalidate等待當前Task結束后關閉。這時Delegate會收到URLSession:didBecomeInvalidWithError:這個事件。Delegate收到這個事件之后會被解引用。
3.NSURLSessionConfiguration類
上面2、3種創建方式使用了NSURLSessionConfiguration,它用于配置會話的屬性,創建方法如下:
+ (NSURLSessionConfiguration *)defaultSessionConfiguration;
+ (NSURLSessionConfiguration *)ephemeralSessionConfiguration;
//identifier參數指定了會話的ID,用于標記后臺的session。
+ (NSURLSessionConfiguration *)backgroundSessionConfiguration:(NSString *)identifier;
這個類有兩個屬性:
@property BOOL allowsCellularAccess;
@property (getter=isDiscretionary) BOOL discretionary NS_AVAILABLE(NA, 7_0);
allowsCellularAccess 屬性指定是否允許使用蜂窩連接, discretionary屬性為YES時表示當程序在后臺運作時由系統自己選擇最佳的網絡連接配置,該屬性可以節省通過蜂窩連接的帶寬。在使用后臺傳輸數據的時候,建議使用discretionary屬性,而不是allowsCellularAccess屬性,因為它會把WiFi和電源可用性考慮在內。
2.實例
創建一個NSURLSession任務需要遵循以下步驟:
a)創建一個Session Configuration
b)創建一個NSURLSession
c) 創建一個NSURLSession Task ( Data,Download or Upload)
d)代理方法
e)調用resume方法
1.數據請求
前面通過請求一個NSURLSessionDataTask進行數據請求演示:
- (IBAction)loadData:(UIButton *)sender {
// 創建Data Task,
NSURL *url = [NSURL URLWithString:@"http://www.lxweimin.com/users/9tsPFp/latest_articles"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error) {
// 輸出返回的狀態碼,請求成功的話為200
if (!error) {
[self showResponseCode:response];
}else{
NSLog(@"error is :%@",error.localizedDescription);
}
}];
// 使用resume方法啟動任務
[dataTask resume];
}
- (void)showResponseCode:(NSURLResponse *)response {
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
NSInteger responseStatusCode = [httpResponse statusCode];
NSLog(@"狀態碼--%ld", (long)responseStatusCode);
}
2.HTTP GET和POST
1).GET
我們使用豆瓣的API來演示這個GET請求
- (IBAction)GETRequest:(id)sender {
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *delegateFreeSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: self delegateQueue: [NSOperationQueue mainQueue]];
NSURL * url = [NSURL URLWithString:@"https://api.douban.com/v2/book/1220562"];
NSURLSessionDataTask * dataTask = [delegateFreeSession dataTaskWithURL:url
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if(error == nil)
{
NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog(@"GET請求數據= %@",text);
}
}];
[dataTask resume];
}
數據請求結果如下:
2).POST
我們使用http://hayageek.com/examples/jquery/ajax-post/ajax-post.php 這個接口來演示POST請求,POST需要4個參數: name, loc, age, submit,如下:
- (IBAction)POSTRequest:(UIButton *)sender {
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *delegateFreeSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];
NSURL * url = [NSURL URLWithString:@"http://hayageek.com/examples/jquery/ajax-post/ajax-post.php"];
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];
NSString * params =@"name=Ravi&loc=India&age=31&submit=true";
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSessionDataTask * dataTask =[delegateFreeSession dataTaskWithRequest:urlRequest
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"Response:%@ %@\n", response, error);
if(error == nil)
{
NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog(@"POST返回數據 = %@",text);
}
}];
[dataTask resume];
}
返回數據:
Response:<NSHTTPURLResponse: 0x7ff948778240> { URL: http://hayageek.com/examples/jquery/ajax-post/ajax-post.php } { status code: 200, headers {
Connection = "keep-alive";
"Content-Encoding" = gzip;
"Content-Type" = "text/html";
Date = "Wed, 18 Mar 2015 01:43:07 GMT";
Server = nginx;
"Transfer-Encoding" = Identity;
Vary = "Accept-Encoding, Accept-Encoding";
"X-Powered-By" = "EasyEngine 2.0.0";
} } (null)
2015-03-18 09:43:10.418 WebConnectionDemo[28129:2666176] POST返回數據 = Data from server: {"name":"Ravi","loc":"India","age":"31","submit":"true"}<br>
3.NSURLSessionUploadTask
Upload task 的創建需要使用一個 request,另外加上一個要上傳的 NSData 對象或者是一個本地文件的路徑對應的 NSURL,類似的代碼如下:
NSURL *URL = [NSURL URLWithString:@"http://example.com/upload"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSData *data = ...;
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request
fromData:data
completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error) {
// ...
}];
[uploadTask resume];
4.NSURLSessionDownloadTask
a.普通下載
- (IBAction)download:(id)sender {
NSURL * url = [NSURL URLWithString:@"http://e.hiphotos.baidu.com/image/pic/item/63d0f703918fa0ec14b94082249759ee3c6ddbc6.jpg"];
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate:self delegateQueue: [NSOperationQueue mainQueue]];
NSURLSessionDownloadTask * downloadTask =[ defaultSession downloadTaskWithURL:url];
[downloadTask resume];
}
為了實現下載進度的顯示,需要在委托中的以下方法中實現:
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
{
float progress = totalBytesWritten*1.0/totalBytesExpectedToWrite;
dispatch_async(dispatch_get_main_queue(),^ {
[self.process setProgress:progress animated:YES];
});
NSLog(@"Progress =%f",progress);
NSLog(@"Received: %lld bytes (Downloaded: %lld bytes) Expected: %lld bytes.\n",
bytesWritten, totalBytesWritten, totalBytesExpectedToWrite);
}
Download task 是將數據一點點地寫入本地的臨時文件。我們需要把文件從一個臨時地址移動到一個永久的地址保存起來::
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
{
NSLog(@"Temporary File :%@\n", location);
NSError *err = nil;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSURL *docsDirURL = [NSURL fileURLWithPath:[docsDir stringByAppendingPathComponent:@"out1.zip"]];
if ([fileManager moveItemAtURL:location
toURL:docsDirURL
error: &err])
{
NSLog(@"File is saved to =%@",docsDir);
}
else
{
NSLog(@"failed to move: %@",[err userInfo]);
}
}
如下所示:
b.可取消的下載
我們將做一些改動,使其支持取消下載,先創建一個全局NSURLSessionDownloadTask對象:
@property (strong, nonatomic) NSURLSessionDownloadTask *cancellableTask;
調用cancle方法即可取消:
- (IBAction)cancleDownload:(UIButton *)sender {
if (self.cancellableTask) {
[self.cancellableTask cancel];
self.cancellableTask = nil;
}
}
這樣點擊cancle按鈕后下載任務會取消,重新點擊下載會從最初的經度條開始下載。
c.斷點續傳下載
斷點續傳,我們需要一個NSData來暫存我們下載的數據:
@property (strong, nonatomic) NSData *partialData;
download方法中做以下改動,如果已經有緩存的數據,即使用downloadTaskWithResumeData進行下載:
- (IBAction)download:(UIButton *)sender {
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate:self delegateQueue: [NSOperationQueue mainQueue]];
if (self.partialData) {
self.cancellableTask = [defaultSession downloadTaskWithResumeData:self.partialData];
}
else{
NSURL * url = [NSURL URLWithString:@"http://e.hiphotos.baidu.com/image/pic/item/63d0f703918fa0ec14b94082249759ee3c6ddbc6.jpg"];
self.cancellableTask =[ defaultSession downloadTaskWithURL:url];
}
[self.cancellableTask resume];
}
cancle方法也需要做改動,我們需要保存我們已下載的數據:
- (IBAction)cancleDownload:(UIButton *)sender {
if (self.cancellableTask) {
[self.cancellableTask cancelByProducingResumeData:^(NSData *resumeData) {
self.partialData = resumeData;
}];
self.cancellableTask = nil;
}
}
另外恢復下載時,NSURLSessionDownloadDelegate中的以下方法將被調用:
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes{
NSLog(@"NSURLSessionDownloadDelegate: Resume download at %lld", fileOffset);
}
如下:
6.后臺下載
首先創建一個全局對象:
@property (strong, nonatomic) NSURLSessionDownloadTask * backgroundDownloadTask;
配置下載任務:
- (IBAction)downoadBackground:(id)sender {
NSURL * url = [NSURL URLWithString:@"https://developer.apple.com/library/ios/documentation/General/Conceptual/CocoaEncyclopedia/CocoaEncyclopedia.pdf"];
NSURLSessionConfiguration * backgroundConfig = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"backgroundtask1"];
NSURLSession *backgroundSeesion = [NSURLSession sessionWithConfiguration: backgroundConfig delegate:self delegateQueue: [NSOperationQueue mainQueue]];
self.backgroundDownloadTask =[ backgroundSeesion downloadTaskWithURL:url];
[self.backgroundDownloadTask resume];
}
在程序進入后臺后,如果下載任務完成,AppDelegate中的對應方法將被調用:
- (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)())completionHandler
{
NSLog(@"Save completionHandler");
self.completionHandler = completionHandler;
}
然后修改上面那個協議方法,如下:
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
{
if (downloadTask == self.cancellableTask) {
NSLog(@"Temporary File :%@\n", location);
NSError *err = nil;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSURL *docsDirURL = [NSURL fileURLWithPath:[docsDir stringByAppendingPathComponent:@"out1.zip"]];
if ([fileManager moveItemAtURL:location
toURL:docsDirURL
error: &err]){
NSLog(@"File is saved to =%@",docsDir);
}
else{
NSLog(@"failed to move: %@",[err userInfo]);
}
}else if(downloadTask == self.backgroundDownloadTask){
NSLog(@"Background URL session %@ finished events.\n", session);
AppDelegate * delegate =(AppDelegate *)[[UIApplication sharedApplication] delegate];
if(delegate.completionHandler)
{
void (^handler)() = delegate.completionHandler;
handler();
}
}
}
運行程序并退出當前程序后打印結果如下:
Received: 21127 bytes (Downloaded: 1439932 bytes) Expected: 1464162 bytes.
Progress =0.997860
Received: 21096 bytes (Downloaded: 1461028 bytes) Expected: 1464162 bytes.
Progress =1.000000
Received: 3134 bytes (Downloaded: 1464162 bytes) Expected: 1464162 bytes.
Background URL session <__NSURLBackgroundSession: 0x7fc5c357b560> finished events.
關于NSURLSessionConfiguration的配置策略你可以在這篇文章中找到。
1.AFNNetworking 2.0
你相信你一定知道AFNNetworking,不知道你還可以看看該作者的博文,所以我就不多說關于它的強大之處了,AFNetworking 提供了比NSURLSession更高層次的抽象,這篇文章主要總結AFNNetworking 2.0的幾個常用方法。
1).GET和POST請求
GET:
- (NSURLSessionDataTask *)GET:(NSString *)URLString
parameters:(id)parameters
success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
POST:
- (NSURLSessionDataTask *)POST:(NSString *)URLString
parameters:(id)parameters
success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
在實際的開發中,我比較習慣把第三方的代碼進行一層封裝,如下我們把GET和POST請求封裝起來:
//GET
+ (void)getWithPath:(NSString *)path params:(NSDictionary *)params success:(HttpSuccessBlock)success failure:(HttpFailureBlock)failure{
[self requestWithPath:path params:params success:success failure:failure method:@"GET"];
}
//POST
+ (void)postWithPath:(NSString *)path params:(NSDictionary *)params success:(HttpSuccessBlock)success failure:(HttpFailureBlock)failure{
[self requestWithPath:path params:params success:success failure:failure method:@"POST"];
}
typedef void (^HttpSuccessBlock)(id JSON);
typedef void (^HttpFailureBlock)(NSError *error);
...省略
+ (void)requestWithPath:(NSString *)path params:(NSDictionary *)params success:(HttpSuccessBlock)success failure:(HttpFailureBlock)failure method:(NSString *)method{
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:kBaseURL]];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"application/json", @"text/json", @"text/javascript", @"text/html", nil];
if ([method isEqual: @"GET"]) {
[manager GET:path parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
success(responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"fail Get!!%@",error);
failure(error);
}];
}else if ([method isEqual:@"POST"]){
[manager POST:path parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"POST成功:%@",responseObject);
success(responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"fail POST!!%@",error);
failure(error);
}];
}
}
你有沒有注意到上面我設置了如下代碼:
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"application/json", @"text/json", @"text/javascript", @"text/html", nil];
因為AFNetworking的responseSerializer 屬性只能默認只能處理特定的Content-Type,如果你想處理"text/html"等其它類型,你需要明確指定它的acceptableContentTypes。
2).多文件上傳
如下,這是我寫的第三方微博客戶端中多圖上傳的實例代碼:
[manager POST:@"2/statuses/upload.json"
parameters:@{@"access_token": accseeToken,
@"status" : encodeStatus,
@"visible" : @(_intVisible),
@"lat" : @(_latitude),
@"long" : @(_longtitude)}
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
NSMutableArray *images = [NSMutableArray arrayWithArray:weakSelf.images];
for (id asset in images) {
NSData *data = nil;
if ([asset isKindOfClass:[UIImage class]]) {
data = UIImageJPEGRepresentation(asset, 0.4);
}
if ([asset isKindOfClass:ALAsset.class]) {
UIImage *original = [UIImage imageWithCGImage: [[asset defaultRepresentation] fullScreenImage]];
data = UIImageJPEGRepresentation(original, 0.4);
}
[formData appendPartWithFileData:data name:@"pic" fileName:@"pic.jpg" mimeType:@"multipart/form-data"];
}
} success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"發送成功");
[self back];
} failure:^(NSURLSessionDataTask *task, NSError *error) {
[self showFailHUD];
}];
3).多線程操作
如果你需要開啟多個線程, 你需要使用AFHTTPRequestSerializer
,AFHTTPRequestOperation和NSOperationQueue
以下是AFNetworking的實例代碼
NSMutableArray *mutableOperations = [NSMutableArray array];
for (NSURL *fileURL in filesToUpload) {
NSURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://example.com/upload" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileURL:fileURL name:@"images[]" error:nil];
}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[mutableOperations addObject:operation];
}
NSArray *operations = [AFURLConnectionOperation batchOfRequestOperations:@[...] progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {
NSLog(@"%lu of %lu complete", numberOfFinishedOperations, totalNumberOfOperations);
} completionBlock:^(NSArray *operations) {
NSLog(@"All operations in batch complete");
}];
[[NSOperationQueue mainQueue] addOperations:operations waitUntilFinished:NO];
3).網絡狀態檢查
網絡狀態檢查在早期都是通過蘋果官方的Reachability類進行檢查,但是這個類本身存在一些問題,并且官方后來沒有再更新。我們可以直接使用AFNetworking框架檢測。不管使用官方提供的類還是第三方框架,用法都是類似的,通常是發送一個URL然后去檢測網絡狀態變化,網絡改變后則調用相應的網絡狀態改變方法。如下:
-(void)alert:(NSString *)message{
UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"System Info" message:message delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles: nil];
[alertView show];
}
-(void)checkNetworkStatus{
//創建一個用于測試的url
NSURL *url=[NSURL URLWithString:@"http://www.apple.com"];
AFHTTPRequestOperationManager *operationManager=[[AFHTTPRequestOperationManager alloc]initWithBaseURL:url];
//根據不同的網絡狀態改變去做相應處理
[operationManager.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch (status) {
case AFNetworkReachabilityStatusReachableViaWWAN:
[self alert:@"2G/3G/4G Connection."];
break;
case AFNetworkReachabilityStatusReachableViaWiFi:
[self alert:@"WiFi Connection."];
break;
case AFNetworkReachabilityStatusNotReachable:
[self alert:@"Network not found."];
break;
default:
[self alert:@"Unknown."];
break;
}
}];
//開始監控
[operationManager.reachabilityManager startMonitoring];
}