AFNetworking簡(jiǎn)單使用
AFNetworking我們?cè)陂_發(fā)中經(jīng)常會(huì)使用到引颈,我這里就根據(jù)自己的使用來說下個(gè)人的認(rèn)識(shí)病线。首先說下AFNetworking的使用:
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"responseObject = %@",responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (error) {
NSLog(@"error = %@",error);
}
}];
上面這種是最簡(jiǎn)單的使用
我們?cè)賮砜聪氯绻孟到y(tǒng)的NSURLSession來做的話是什么樣的温学,如下
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
request.HTTPMethod = @"GET";
[request setValue:@"application/json" forHTTPHeaderField:@"Conten-Type"];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if(error){
NSLog(@"error = %@",error);
}else{
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
NSLog(@"dic = %@",dic);
}
}];
[task resume];
從調(diào)用步驟上看宙枷,使用AFNetworking比我們直接使用系統(tǒng)的NSURLSession要方便點(diǎn)多,那AFNetworking內(nèi)部到底是如何做的呢蕴忆?下面我們就通過源碼看下颤芬。
AFNetworking組成部分
-NSURLSession :網(wǎng)絡(luò)處理,請(qǐng)求套鹅、上傳站蝠、下載
-Reachability:網(wǎng)絡(luò)可達(dá)性,檢測(cè)網(wǎng)絡(luò)變化
-Security:完全相關(guān)卓鹿,處理請(qǐng)求時(shí)的簽名等處理
-Serialization:序列化菱魔,分為request和response
AFNetworking內(nèi)部實(shí)現(xiàn)
我們主要看下NSURLSession這部分是如何設(shè)計(jì)和實(shí)現(xiàn)的。
NSURLSession由兩個(gè)類組成:AFHTTPSessionManager和AFURLSessionManager吟孙。
AFHTTPSessionManager
繼承于AFURLSessionManager澜倦,是AFURLSessionManager進(jìn)行了二次封裝。主要是我們常用的get杰妓、post請(qǐng)求肥隆。
AFHTTPSessionManager
繼承于NSObject,是對(duì)NSURLSession的封裝稚失。封裝了請(qǐng)求、上傳恰聘、下載等相關(guān)方法句各。
上面的各種方法都是通過NSURLSession對(duì)應(yīng)的delegate方式來實(shí)現(xiàn)的吸占。
關(guān)鍵的一個(gè)類AFURLSessionManagerTaskDelegate,這個(gè)是用來記錄上傳時(shí)相關(guān)NSURLSessionTask信息的凿宾,具體定義如下:
@interface AFURLSessionManagerTaskDelegate : NSObject <NSURLSessionTaskDelegate, NSURLSessionDataDelegate, NSURLSessionDownloadDelegate>
- (instancetype)initWithTask:(NSURLSessionTask *)task;
@property (nonatomic, weak) AFURLSessionManager *manager;
@property (nonatomic, strong) NSMutableData *mutableData;
@property (nonatomic, strong) NSProgress *uploadProgress;
@property (nonatomic, strong) NSProgress *downloadProgress;
@property (nonatomic, copy) NSURL *downloadFileURL;
@property (nonatomic, copy) AFURLSessionDownloadTaskDidFinishDownloadingBlock downloadTaskDidFinishDownloading;
@property (nonatomic, copy) AFURLSessionTaskProgressBlock uploadProgressBlock;
@property (nonatomic, copy) AFURLSessionTaskProgressBlock downloadProgressBlock;
@property (nonatomic, copy) AFURLSessionTaskCompletionHandler completionHandler;
@end
當(dāng)用戶調(diào)用請(qǐng)求接口時(shí)矾屯,會(huì)創(chuàng)建AFURLSessionManagerTaskDelegate對(duì)象,然后添加到NSMutableDictionary對(duì)象mutableTaskDelegatesKeyedByTaskIdentifier中初厚,用于后面不同sessionTask的區(qū)分件蚕。
當(dāng)數(shù)據(jù)接收完成對(duì)應(yīng)代碼:
//代理
- (void)URLSession:(NSURLSession *)session
task:(NSURLSessionTask *)task
didCompleteWithError:(NSError *)error
{
AFURLSessionManagerTaskDelegate *delegate = [self delegateForTask:task];
// delegate may be nil when completing a task in the background
if (delegate) {
[delegate URLSession:session task:task didCompleteWithError:error];
[self removeDelegateForTask:task];
}
if (self.taskDidComplete) {
self.taskDidComplete(session, task, error);
}
}
請(qǐng)求完成后,清空task數(shù)據(jù)
- (void)removeDelegateForTask:(NSURLSessionTask *)task {
NSParameterAssert(task);
[self.lock lock];
[self removeNotificationObserverForTask:task];
[self.mutableTaskDelegatesKeyedByTaskIdentifier removeObjectForKey:@(task.taskIdentifier)];
[self.lock unlock];
}