//
// netConnection.m
// uiNSURLas
//
// Created by lh2718 on 16/4/5.
// Copyright © 2016年 lh2718. All rights reserved.
//
#import "netConnection.h"
@interface netConnection ()<NSURLSessionDataDelegate,NSURLSessionDelegate>
@property (nonatomic, retain)NSMutableData *data;
@property (nonatomic, assign)NSInteger identifier;
@end
@implementation netConnection
-(void)dealloc
{
[_data release];
[super dealloc];
}
-(instancetype)init
{
self = [super init];
if(self)
{
self.data = [NSMutableData data];
}
return self;
}
+(instancetype)netConnectionWithURL:(NSString *)strURL parameters:(NSDictionary *)paramenters delegate:(id<netConnectionDelegate>)delegate identifier:(NSInteger)identifier
{
NSString *par = @"";
//拼接网址
for(NSString *key in paramenters)
{
if([par length] == 0)
{
par = [NSString stringWithFormat:@"?%@=%@",key,[paramenters objectForKey:key]];
}
else{
par = [NSString stringWithFormat:@"%@&%@=%@",par,key,[paramenters objectForKey:key]];
}
}
strURL = [NSString stringWithFormat:@"%@%@",strURL,par];
netConnection *netConn = [[netConnection alloc]init];
netConn.delegate = delegate;
netConn.identifier = identifier;
[netConn startURLSession:strURL];
return [netConn autorelease];
}
-(void)startURLSession:(NSString *)strURL
{
NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:[NSOperationQueue mainQueue]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:strURL]];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request];
[dataTask resume];
}
-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler
{
completionHandler(NSURLSessionResponseAllow);
}
-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
{
[self.data appendData:data];
}
-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
[self.delegate connectionFinish:_data identifier:_identifier];
}
@end
// netConnection.m
// uiNSURLas
//
// Created by lh2718 on 16/4/5.
// Copyright © 2016年 lh2718. All rights reserved.
//
#import "netConnection.h"
@interface netConnection ()<NSURLSessionDataDelegate,NSURLSessionDelegate>
@property (nonatomic, retain)NSMutableData *data;
@property (nonatomic, assign)NSInteger identifier;
@end
@implementation netConnection
-(void)dealloc
{
[_data release];
[super dealloc];
}
-(instancetype)init
{
self = [super init];
if(self)
{
self.data = [NSMutableData data];
}
return self;
}
+(instancetype)netConnectionWithURL:(NSString *)strURL parameters:(NSDictionary *)paramenters delegate:(id<netConnectionDelegate>)delegate identifier:(NSInteger)identifier
{
NSString *par = @"";
//拼接网址
for(NSString *key in paramenters)
{
if([par length] == 0)
{
par = [NSString stringWithFormat:@"?%@=%@",key,[paramenters objectForKey:key]];
}
else{
par = [NSString stringWithFormat:@"%@&%@=%@",par,key,[paramenters objectForKey:key]];
}
}
strURL = [NSString stringWithFormat:@"%@%@",strURL,par];
netConnection *netConn = [[netConnection alloc]init];
netConn.delegate = delegate;
netConn.identifier = identifier;
[netConn startURLSession:strURL];
return [netConn autorelease];
}
-(void)startURLSession:(NSString *)strURL
{
NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:[NSOperationQueue mainQueue]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:strURL]];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request];
[dataTask resume];
}
-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler
{
completionHandler(NSURLSessionResponseAllow);
}
-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
{
[self.data appendData:data];
}
-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
[self.delegate connectionFinish:_data identifier:_identifier];
}
@end