您的位置:首页 > 移动开发 > IOS开发

ios 基于NSConnection简单封装的工具类

2015-10-15 11:11 579 查看
接口文件

//
//  HTTPRequest.h
//
//  Created by Jason_Msbaby on 15/10/9.
//  Copyright © 2015年 张杰. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

typedef NS_ENUM(NSInteger, HttpMethod) {
HttpMethodGet       = 0,
HttpMethodPost   =  1
};
/**
*  封装了NSURLCollection网络请求
返回的回调函数会在主线程中执行
*/
@interface HTTPRequest : NSObject
/**
*  获取实例
*
*  @return 返回当前单例对象
*/
+(instancetype)defaultRequest;
/**
*  发送请求的方法
*
*  @param url        请求地址 字符串类型
*  @param method     请求类型  get post 枚举类型
*  @param parameters 参数 不论是get请求 或者是post请求  都存放于此  字典类型
*  @param headers    请求头  字典类型的键值对
*  @param success    返回成功的回调函数
*  @param failure    返回失败的回调函数
*  @param finish     无论返回成功或者失败 都会触发的回调函数
*/
-(void)sendWithUrl:(NSString*)url Method:(HttpMethod)method Parameters:(NSDictionary*)parameters Headers:(NSDictionary *)headers Success:(void (^)(NSDictionary *data))success Failure:(void(^)(NSError *error))failure Finish:(void(^)())finish;

@end


实现部分
//
//  HTTPRequest.m
//  douBan
//
//  Created by Jason_Msbaby on 15/10/9.
//  Copyright © 2015年 张杰. All rights reserved.
//

#import "HTTPRequest.h"

static HTTPRequest *request;

@interface HTTPRequest ()
@end

@implementation HTTPRequest

+(instancetype)defaultRequest{
if (request == nil) {
request = [HTTPRequest new];
}
return request;
}

-(void)sendWithUrl:(NSString*)url Method:(HttpMethod)method Parameters:(NSDictionary*)parameters Headers:(NSDictionary *)headers Success:(void (^)(NSDictionary *data))success Failure:(void(^)(NSError *error))failure Finish:(void(^)())finish {
//获取参数内容
NSString *para = nil;
if (parameters != nil) {
NSMutableString *paraString = [NSMutableString string];
for (int i = 0; i < parameters.count; i++) {
NSString *key = [NSString stringWithFormat:@"%@",parameters.allKeys[i]];
NSString *value = [NSString stringWithFormat:@"%@",parameters.allValues[i]];
[paraString appendString:key];
[paraString appendString:@"="];
[paraString appendString:value];
[paraString appendString:@"&"];
}
para = [paraString substringToIndex:paraString.length-1];
}
//封装request
NSMutableURLRequest *req = nil;
if (method == HttpMethodPost) {
req.HTTPMethod = @"POST";
NSURL *nsurl = [NSURL URLWithString:url];
req = [NSMutableURLRequest requestWithURL:nsurl];
NSData *paraData = [para dataUsingEncoding:NSUTF8StringEncoding];
[req setHTTPBody:paraData];

}else if(method == HttpMethodGet){
req.HTTPMethod = @"GET";
NSURL *nsurl = [NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",url,para]];
req = [NSMutableURLRequest requestWithURL:nsurl];
}
//封装header
if (headers != nil) {
for (int i = 0; i < headers.count; i++) {
NSString *key = headers.allKeys[i];
NSString *value = headers.allValues[i];
[req addValue:value forHTTPHeaderField:key];
}
}
//发送请求
[NSURLConnection sendAsynchronousRequest:req queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {

NSHTTPURLResponse *res = (NSHTTPURLResponse*)response;

if ( connectionError || res.statusCode != 200) {
if (failure != nil) {
failure(connectionError);
}

}
if (res.statusCode == 200) {
if (success!=nil) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

success(dic);
}

}
if (finish != nil) {
finish();
}

}];
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息