Created
April 6, 2011 04:49
-
-
Save splhack/905154 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "AsyncURLConnection.h" | |
@implementation AsyncURLConnection | |
+ (id)request:(NSString *)requestUrl completeBlock:(completeBlock_t)completeBlock errorBlock:(errorBlock_t)errorBlock | |
{ | |
return [[[self alloc] initWithRequest:requestUrl | |
completeBlock:completeBlock errorBlock:errorBlock] autorelease]; | |
} | |
- (id)initWithRequest:(NSString *)requestUrl completeBlock:(completeBlock_t)completeBlock errorBlock:(errorBlock_t)errorBlock | |
{ | |
if ((self=[super init])) { | |
data_ = [[NSMutableData alloc] init]; | |
completeBlock_ = [completeBlock copy]; | |
errorBlock_ = [errorBlock copy]; | |
NSURL *url = [NSURL URLWithString:requestUrl]; | |
NSURLRequest *request = [NSURLRequest requestWithURL:url]; | |
[NSURLConnection connectionWithRequest:request delegate:self]; | |
} | |
return self; | |
} | |
- (void)dealloc | |
{ | |
[data_ release]; | |
[completeBlock_ release]; | |
[errorBlock_ release]; | |
[super dealloc]; | |
} | |
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response | |
{ | |
[data_ setLength:0]; | |
} | |
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data | |
{ | |
[data_ appendData:data]; | |
} | |
- (void)connectionDidFinishLoading:(NSURLConnection *)connection | |
{ | |
completeBlock_(data_); | |
} | |
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error | |
{ | |
errorBlock_(error); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment