#import <Foundation/Foundation.h>//.h 中的文件
@class NetWorkManger;
@protocol NetWorkMangerDelegate <NSObject>
@optional
- (void)getDataSuccess:(NetWorkManger *)netWork object:(id)obj;
- (void)getdataFail:(NetWorkManger *)netWork error:(NSError *)error;
@end
//Block 是匿名函数
//Typedf 类型重名;
//定义了一个参数为(NetWorkManger,id) 返回值为 void 函数的类型函数,类型重名为 successBlock;
typedef void (^successBlock) (NetWorkManger *net,id object);
typedef void (^failBlock) (NetWorkManger *net,NSError *error);
@interface NetWorkManger : NSObject
@property (nonatomic,assign) id<NetWorkMangerDelegate>delegate;
+ (NetWorkManger *)mainNetworkManager;
- (void)getDateWithURL:(NSString *)urlStr parameter:(NSString *)parameter;
- (void)getDateWithURL:(NSString *)urlStr success:(successBlock)success fail:(failBlock)fail;
@end
#import "NetWorkManger.h"
@interface NetWorkManger ()<NSURLConnectionDataDelegate,NSURLConnectionDelegate>
@property (nonatomic,strong) NSURLConnection *connection;
@property (nonatomic,strong) NSMutableData *data;
@end
@implementation NetWorkManger
+ (NetWorkManger *)mainNetworkManager {
static NetWorkManger *networkManager = nil;
@synchronized (self) {
if (!networkManager) {
networkManager = [[NetWorkManger alloc] init];
}
return networkManager;
}
}
- (void)getDateWithURL:(NSString *)urlStr parameter:(NSString *)parameter{
NSURL *url = [NSURL URLWithString:urlStr];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//设置请求类型;
request.HTTPMethod = @"POST";
//转换类型
request.HTTPBody = [parameter dataUsingEncoding:NSUTF8StringEncoding];
//开启请求
self.connection = [NSURLConnection connectionWithRequest:request delegate:self];
}
//服务器接收相应
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
self.data = [NSMutableData data];
}
//收到数据
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.data appendData:data];
}
//结束加载
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
//解析数据
id obj = [NSJSONSerialization JSONObjectWithData:self.data options:NSJSONReadingMutableContainers error:nil];
//先判断 delegate 是否存在;
//然后判断 delegate 是否响应;
if (self.delegate && [self.delegate respondsToSelector:@selector(getDataSuccess:object:)]) {
[self.delegate getDataSuccess:self object:obj];
}
}
//请求失败;
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
if (self.delegate && [self.delegate respondsToSelector:@selector(getdataFail:error:)]) {
[self.delegate getdataFail:self error:nil];
}
}
- (void)getDateWithURL:(NSString *)urlStr success:(successBlock)success fail:(failBlock)fail{
NSURL *url = [NSURL URLWithString:urlStr];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[ NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
//若果 data 存在,解析,回调
//反之,失败回调;
if (data) {
id obj = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
success(self,obj);
}else{
fail(self,connectionError);
}
}];
}
@end