LGNetworking
链式网络管理工具(基于AFNetworking),支持GET、POST、TEXT、UPLOAD、DOWNLOAD、PUT、DELETE、PATCH
使用方式
1、安装
pod 'LGNetworking'
2、代码调用
GET:
NSString *url = @"http://ip:port/xxx?xx=xx";
NSDictionary *params = @{@"userID":@"10086",
@"subjectID":@"S2-English"};
NSDictionary *httpHeader = @{@"secret":@"9878937873587348578454759435"};
[LGN.setRequestUrl(url).setRequestType(GET).setParameters(params).setHTTPHeader(httpHeader) startRequestWithSuccess:^(id response) {
// 请求成功
} failure:^(NSError *error) {
// 请求失败
}];
POST:
NSString *url = @"http://ip:port/xxx";
NSDictionary *params = @{@"userID":@"10086",
@"subjectID":@"S2-English"};
NSDictionary *httpHeader = @{@"secret":@"9878937873587348578454759435"};
[LGN.setRequestUrl(url).setRequestType(POST).setParameters(params).setHTTPHeader(httpHeader) startRequestWithSuccess:^(id response) {
// 请求成功
} failure:^(NSError *error) {
// 请求失败
}];
TXET(加载本地文本资料):
NSString *path = @"file://xxx/xxx.txt";
[LGN.setRequestPath(path).setRequestType(TEXT) startRequestWithSuccess:^(id response) {
// 请求成功
} failure:^(NSError *error) {
// 请求失败
}];
UPLOAD:
LGUploadModel *uploadModel = [[LGUploadModel alloc] init];
UIImage *image = [UIImage imageNamed:@"xx"];
uploadModel.datas = @[UIImageJPEGRepresentation(image, 0.5)];
uploadModel.fileNames = @[[NSString stringWithFormat:@"照片%.f.png",[[NSDate date] timeIntervalSince1970]]];
uploadModel.fileType = @"image/png";
NSString *url = @"http://ip:port/xxx";
[LGN.setRequestUrl(url).setRequestType(UPLOAD).setUploadModel(uploadModel) startRequestWithProgress:^(NSProgress *progress) {
// 上传进度
} success:^(id response) {
// 上传成功
} failure:^(NSError *error) {
// 上传失败
}];
DOWNLOAD:
NSString *url = @"http://ip:port/xxx";
NSString *savePath = @"file://xxx/xxx.txt";
[LGN.setRequestUrl(url).setRequestType(DOWNLOAD) startRequestWithProgress:^(NSProgress *progress) {
// 下载进度
} success:^(id response) {
// 下载成功
} failure:^(NSError *error) {
// 下载失败
}];