Skip to content

Instantly share code, notes, and snippets.

@ankitthakur
Created November 21, 2013 06:37
Show Gist options
  • Save ankitthakur/7576970 to your computer and use it in GitHub Desktop.
Save ankitthakur/7576970 to your computer and use it in GitHub Desktop.
#import "NetworkParams.h"
@interface Network : NSObject <NSURLConnectionDelegate, NSURLConnectionDataDelegate>
- (void) initWithRequest:(NSURLRequest*)request onCompletion:(CompletionBlock)success onError:(ErrorBlock)error;
@end
#import "Network.h"
@interface Network ()
@property (strong, nonatomic) NSMutableArray *networkParams;
@end
@implementation Network
- (void) initWithRequest:(NSURLRequest*)request onCompletion:(CompletionBlock)success onError:(ErrorBlock)error{
NetworkParams *param = [[NetworkParams alloc] init];
param.request = request;
param.completionBlock = success;
param.errorBlock = error;
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
param.connection = connection;
[_networkParams addObject:param];
[connection start];
}
#pragma marks - NSURLConnectionDelegate
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
HDNetworkParams *param = [[_networkParams filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"connection = %@", connection]] lastObject];
if (param) {
if (param.errorBlock) {
param.errorBlock(error);
}
}
}
- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection{
return NO;
}
- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{
}
- (void)connection:(NSURLConnection *)connection didCancelAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{
}
#pragma marks - NSURLConnectionDataDelegate
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
HDNetworkParams *param = [[_networkParams filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"connection = %@", connection]] lastObject];
if (param) {
param.responseData = data;
}
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
NetworkParams *param = [[_networkParams filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"connection = %@", connection]] lastObject];
if (param) {
if (param.completionBlock) {
param.completionBlock(param.responseData);
}
}
}
@end
#import <Foundation/Foundation.h>
typedef void (^CompletionBlock)(id responseData);
typedef void (^ErrorBlock)(NSError* error);
@interface NetworkParams : NSObject
@property (nonatomic, strong) NSURLConnection *connection;
@property (nonatomic, strong) NSURLRequest *request;
@property (nonatomic, strong) CompletionBlock completionBlock;
@property (nonatomic, strong) ErrorBlock errorBlock;
@property (nonatomic, assign) id responseData;
@end
#import "NetworkParams.h"
@implementation NetworkParams
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment