-
-
Save coylums/1302137 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 | |
{ | |
NSURL *url = [NSURL URLWithString:requestUrl]; | |
NSURLRequest *request = [NSURLRequest requestWithURL:url]; | |
if ((self = [super | |
initWithRequest:request delegate:self startImmediately:NO])) { | |
data_ = [[NSMutableData alloc] init]; | |
completeBlock_ = [completeBlock copy]; | |
errorBlock_ = [errorBlock copy]; | |
[self start]; | |
} | |
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