iOS开发组件YNKit之网络数据请求
该组件由我和占宇同事共同开发的iOS开发高效的一个组件集,在快捷开发中提炼出来的一系列组件集,改组件集目前覆盖网络层数据请求、展示,自定义cell布局列表,ApiService类,工具类,视图类等
下面一一说明各自部分的作用和用法
网络层
1 YNRequest 【请求类】
2 ApiService 【数据Api类】
ApiService类里封装的是YNRequest的封装方法,ApiService的作用就是抽离Controller和网络请求层的数据展示,如下:
+ (void)apiWithInspecList:(UITableView *)tableView
pageNum:(NSInteger)pageNum
arrProduct:(NSMutableArray *)arrProduct
pageChange:(PageChangeBlock)pageChange
repeatArr:(RepeatBlock)repeatBlock{
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
[dictionary setValue:[utils getUnitID] forKey:@"unitId"];
[dictionary setValue:[utils getlogName] forKey:@"userAccnt"];
[dictionary setValue:@"" forKey:@"pointName"];
[dictionary setValue:[NSString stringWithFormat:@"%zd",pageNum] forKey:@"pageNo"];
[YNRequest YNPost:LBS_QueryIspePoint parameters:dictionary success:^(NSDictionary *dic) {
//处理YNRequest的dictionary
} fail:^{
[SVProgressHUD showInfoWithStatus:@"网络异常"];
}];
接下来看YNRequest的数据请求类
+ (void)YNPost:(NSString *)URLString
parameters:(NSDictionary *)parameters
success:(SuccessBlock)returnBlock
fail:(FailBlock)failBlock {
//参数字典转字符串
NSString *value;
BOOL isValidJSONObject = [NSJSONSerialization isValidJSONObject:parameters];
if (isValidJSONObject) {
NSData *data = [NSJSONSerialization dataWithJSONObject:parameters options:kNilOptions error:nil];
value = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
}
NSMutableArray *params = [NSMutableArray array];
[params addObject:[NSDictionary dictionaryWithObjectsAndKeys:value,@"input",nil]];
//请求方式、请求head、请求地址等设置
YNRequestWithArgs *args = [[YNRequestWithArgs alloc] init];
args.methodName = URLString;
args.soapParams = params;
args.httpWay = ServiceHttpSoap1;
//AFNetWorking 请求网络数据
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager HTTPRequestOperationWithArgs:args success:^(AFHTTPRequestOperation *operation, id responseObject) {
//解析XML
NSString *theXML = operation.responseString ;
NSArray *array1 = [NSArray arrayWithObjects:@"return>",@"</",nil];
NSArray *ziFuArray = [NSArray arrayWithObjects:array1,nil];
for (NSArray *array in ziFuArray) {
NSRange range = [theXML rangeOfString:[array objectAtIndex:0]];
if(range.length > 0)
{
theXML = [theXML substringFromIndex:range.location + 7];
range = [theXML rangeOfString:[array objectAtIndex:1]];
if(range.length > 0)
{
theXML = [theXML substringToIndex:range.location+(range.length - 2)];
}
break;
}
}
//返回字典给ApiService类
NSDictionary *diction = [NSJSONSerialization JSONObjectWithData:[theXML dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:nil];
returnBlock(diction);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
failBlock();
}];
接下来就看Controller怎样请求,直接使用ApiService类方法,如下:
//获取巡检点列表
⁃ (void)getInspecList {
[InspectionApi apiWithInspecList:self.mytb pageNum:self.pageNum arrProduct:self.arrInspection pageChange:^{
self.pageNum -= 1;
} repeatArr:^{
}];
一个回调方法,就让self.arrProduct这个数组接收到值。从而可以进一步操作这个数组了。
GitHub代码地址:https://github.com/PowerYang/YNRequest
网友评论