Skip to content

Instantly share code, notes, and snippets.

@corosukeK
Created August 8, 2012 09:17
Show Gist options
  • Save corosukeK/3293701 to your computer and use it in GitHub Desktop.
Save corosukeK/3293701 to your computer and use it in GitHub Desktop.
HTTPLoader
//
// HTTPLoader
//
// Created by Keisuke Kimura on 2012/08/08.
// Copyright (c) 2012年 Keisuke Kimura. All rights reserved.
//
//
// HTTPLoader.h
//
#import <Foundation/Foundation.h>
#define kHTTPLoaderConnectionCancel @"HTTPLoaderConnectionCancel"
typedef void(^CompleteBlock)(NSData *data, NSError *error);
@interface HTTPLoader : NSObject
+ (void)loadAsync:(NSURLRequest*)request
complete:(CompleteBlock)completeBlock;
+ (void)cancelAll;
@end
//
// HTTPLoaderConnection.h
//
@interface HTTPLoaderConnection : NSObject
- (id)initWithRequest:(NSURLRequest*)request
completeBlock:(CompleteBlock)completeBlock;
- (void)start;
@end
// HTTPLoaderConnection.m
@interface HTTPLoaderConnection ()<NSURLConnectionDelegate>
{
NSMutableData* _receivedData;
NSURLConnection* _connection;
}
@property (nonatomic, strong) NSURLRequest* request;
@property (nonatomic, copy) CompleteBlock requestCompleteBlock;
@end
@implementation HTTPLoaderConnection
- (id)initWithRequest:(NSURLRequest*)request
completeBlock:(CompleteBlock)completeBlock
{
self = [super init];
if(self){
self.request = request;
self.requestCompleteBlock = completeBlock;
[self setCancelObserver];
}
return self;
}
- (void)start
{
_receivedData = [[NSMutableData alloc] init];
_connection = [NSURLConnection connectionWithRequest:self.request delegate:self];
[_connection start];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
NSHTTPURLResponse *httpURlResponse = (NSHTTPURLResponse *)response;
NSInteger statusCode = httpURlResponse.statusCode;
if (statusCode >= 400) {
[connection cancel];
NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:statusCode userInfo:nil];
[self connection:connection didFailWithError:error];
}
}
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[_receivedData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
if (self.requestCompleteBlock) {
self.requestCompleteBlock(_receivedData, nil);
}
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
if (self.requestCompleteBlock) {
self.requestCompleteBlock(nil, error);
}
}
- (void)setCancelObserver
{
NSNotificationCenter* defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self selector:@selector(cancelConnection) name:kHTTPLoaderConnectionCancel object:nil];
}
- (void)removeCancelObserver
{
NSNotificationCenter* defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter removeObserver:self name:kHTTPLoaderConnectionCancel object:nil];
}
- (void)cancelConnection
{
[_connection cancel];
}
- (void)dealloc
{
[self removeCancelObserver];
}
@end
//
// HTTPLoader.m
//
@implementation HTTPLoader
+ (void)loadAsync:(NSURLRequest *)request complete:(CompleteBlock)completeBlock
{
HTTPLoaderConnection* operation = [[HTTPLoaderConnection alloc] initWithRequest:request
completeBlock:completeBlock];
[operation start];
}
+ (void)cancelAll
{
NSNotification* notification = [NSNotification notificationWithName:kHTTPLoaderConnectionCancel object:nil];
NSNotificationCenter* defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter postNotification:notification];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment