概述
如今大多App都會(huì)與網(wǎng)絡(luò)打交道,作為開發(fā)者,合理的對(duì)網(wǎng)絡(luò)后臺(tái)請(qǐng)求接口進(jìn)行封裝十分重要。本文要介紹的就是一種常見的采用回調(diào)函數(shù)(方法)的網(wǎng)絡(luò)接口封裝,也算的是一種構(gòu)架吧。
這個(gè)構(gòu)架主要的idea是這樣的,把所有的接口封裝成一個(gè)類,在工程中隨時(shí)可以調(diào)用。并且利用代理Delegate構(gòu)建回調(diào)方法(callBack),工程中隨處可以通過(guò)回調(diào)方法監(jiān)聽網(wǎng)絡(luò)請(qǐng)求的反饋,也就是說(shuō),一旦得到了服務(wù)器反饋的數(shù)據(jù),回調(diào)函數(shù)中的代碼就(才)會(huì)被激活。網(wǎng)絡(luò)請(qǐng)求基于AFNetworking(AFNetworking,非常有名的網(wǎng)絡(luò)請(qǐng)求第三方類庫(kù)),請(qǐng)求均為異步。如此構(gòu)架,非常靈活很容易擴(kuò)展和復(fù)用。
講解
要想使用本文介紹的構(gòu)架,你首先需要掌握代理(Delegate),如果你不熟悉代理,這個(gè)構(gòu)架對(duì)你來(lái)說(shuō)將會(huì)很不解。對(duì)于不熟悉代理的同學(xué)們,建議你們?nèi)タ匆幌沦Y料。網(wǎng)絡(luò)請(qǐng)求其實(shí)說(shuō)白了就是和服務(wù)器做一個(gè)數(shù)據(jù)交互,App把請(qǐng)求數(shù)據(jù)發(fā)給服務(wù)器,服務(wù)器返回給App一個(gè)反饋數(shù)據(jù)。請(qǐng)先看一下這個(gè)構(gòu)架的示意圖,如下:

QPNet.h
#import <Foundation/Foundation.h>
#import "AFNetworking.h"
// 代理
@protocol QPNetDelegate <NSObject>
/**
* 代理回調(diào)方法:一個(gè)請(qǐng)求成功、一個(gè)請(qǐng)求失敗。
*
* @param feedbackInfo 服務(wù)器返回的數(shù)據(jù)
*/
- (void)submitLocationSuccessFeedback:(id)feedbackInfo;
- (void)submitLocationFailFeedback:(id)failInfo;
@end
@interface QPNet : NSObject
// Delegate的核心的作用就是來(lái)實(shí)現(xiàn)類之間的數(shù)據(jù)傳遞
@property (nonatomic,strong) id<QPNetDelegate> delegate;
/**
* 獲取Net類的單例
*
* @return Net類的單例 實(shí)例(對(duì)象)
*/
+ (QPNet *)getInstance;
/**
* @author chenqianping
*
* 提交個(gè)人位置接口
* @param phone 手機(jī)號(hào)碼
* @param lat 緯度
* @param lon 經(jīng)度
* @param address 地址
* @param type 簽到類型:1 手工簽到,自動(dòng)簽到
*
* @return
*/
- (void)submitLocation: (NSString *)phone
lat :(NSString *)lat
lon :(NSString *)lon
address :(NSString *)address
type :(NSString *)type;
@end
QPNet.m
#import "QPNet.h"
#import "UrlDefine.h"
__strong static AFHTTPRequestOperationManager *AFHTTPMgr;
__strong static QPNet *NetInstance = nil;
@implementation QPNet
+ (QPNet *)getInstance
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken,^{
// 初始化實(shí)例
NetInstance= [[QPNet alloc] init];
AFHTTPMgr = [AFHTTPRequestOperationManager manager];
AFHTTPMgr.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
AFHTTPMgr.responseSerializer = [AFHTTPResponseSerializer serializer];
});
return NetInstance;
}
- (void)submitLocation:(NSString *)phone lat:(NSString *)lat lon:(NSString *)lon address:(NSString *)address type:(NSString *)type
{
NSUserDefaults *appDefault =[NSUserDefaults standardUserDefaults];
phone = [appDefault objectForKey:@"phone"];
NSDictionary *dict = @{ @"a_infophone":phone,@"latitude":lat,@"longitude":lon,@"address":address,@"type":type};
[AFHTTPMgr GET:POSITION_URI parameters:dict success:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) {
// 請(qǐng)求成功Block,將返回?cái)?shù)據(jù)傳入代理方法
[self.delegate submitLocationSuccessFeedback:responseObject];
} failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
// 請(qǐng)求失敗Blick,將錯(cuò)誤信息傳入代理方法
[self.delegate submitLocationFailFeedback:error];
}];
}
@end