Created
January 22, 2011 14:28
-
-
Save valexa/791151 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
@interface VAImage : UIView <VAUrlConnectionDelegate> { | |
VAUrlConnection *conn; | |
} | |
- (void) connectionDidFinish:(VAUrlConnection *)theConnection; | |
@implementation VAImage | |
- (id)initWithFrame:(CGRect)theFrame{ | |
self = [super initWithFrame:theFrame]; | |
if (self) { | |
conn = [[VAUrlConnection alloc] initWithURL:theUrl delegate:self]; | |
conn.name = filename; | |
NSLog(@"Downloading %@ .",theUrl); | |
} | |
return self; | |
} | |
- (void)dealloc{ | |
[conn.theConnection cancel]; | |
[conn release]; | |
[super dealloc]; | |
} | |
- (void) connectionDidFinish:(VAUrlConnection *)theConnection{ | |
NSLog(@"Got %@",theConnection.url); | |
} | |
@protocol VAUrlConnectionDelegate; | |
@interface VAUrlConnection : NSObject { | |
id <VAUrlConnectionDelegate> delegate; | |
NSString *url; | |
NSMutableData *receivedData; | |
NSURLConnection *theConnection; | |
NSString *name; | |
int statusCode; | |
} | |
@property (nonatomic, copy) NSString *url; | |
@property (nonatomic, assign) id<VAUrlConnectionDelegate> delegate; | |
@property (nonatomic, retain) NSMutableData *receivedData; | |
@property (nonatomic, retain) NSURLConnection *theConnection; | |
@property (nonatomic, copy) NSString *name; | |
@property (nonatomic, assign) int statusCode; | |
- (id) initWithURL:(NSString*)theURL delegate:(id<VAUrlConnectionDelegate>)theDelegate; | |
@end | |
@protocol VAUrlConnectionDelegate<NSObject> | |
@required | |
- (void) connectionDidFinish:(VAUrlConnection *)theConnection; | |
@optional | |
- (void) connectionDidFail:(VAUrlConnection *)theConnection; | |
@end | |
@implementation VAUrlConnection | |
@synthesize delegate,url,receivedData,theConnection,name,statusCode; | |
- (id) initWithURL:(NSString*)theURL delegate:(id<VAUrlConnectionDelegate>)theDelegate | |
{ | |
if (self = [super init]) { | |
self.delegate = theDelegate; | |
self.url = theURL; | |
NSURL *newURL = [NSURL URLWithString:theURL]; | |
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:newURL cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:30]; | |
receivedData = [[NSMutableData alloc] initWithLength:0]; | |
/* Create the connection with the request and start loading the data. The connection object is owned both by the creator and the loading system. */ | |
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES]; | |
if (conn){ | |
//NSLog(@"UrlConnection for %@ started",url); | |
}else { | |
NSLog(@"The NSURLConnection could not be made!..."); | |
} | |
self.theConnection = conn; | |
[conn release]; | |
} | |
return self; | |
} | |
- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response | |
{ | |
/* This method is called when the server has determined that it has | |
enough information to create the NSURLResponse. It can be called | |
multiple times, for example in the case of a redirect, so each time | |
we reset the data. */ | |
[self.receivedData setLength:0]; | |
self.statusCode = [((NSHTTPURLResponse *)response) statusCode]; | |
//NSLog(@"Got response %i for %@",statusCode,[response URL]); | |
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES; | |
} | |
- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data | |
{ | |
/* Append the new data to the received data. */ | |
[self.receivedData appendData:data]; | |
} | |
- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error | |
{ | |
NSLog(@"UrlConnection failed (%@)",[error localizedDescription]); | |
if ( self.delegate != nil && [self.delegate respondsToSelector:@selector(connectionDidFail:)] ) { | |
[self.delegate connectionDidFail:self]; | |
} | |
self.theConnection = nil;// Release the connection now that it's finished | |
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO; | |
} | |
- (void) connectionDidFinishLoading:(NSURLConnection *)connection | |
{ | |
if ( self.delegate != nil && [self.delegate respondsToSelector:@selector(connectionDidFinish:)] ) { | |
[self.delegate connectionDidFinish:self]; | |
} | |
self.theConnection = nil;// Release the connection now that it's finished | |
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO; | |
} | |
- (NSCachedURLResponse *) connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse | |
{ | |
/* this application does not use a NSURLCache disk or memory cache */ | |
return nil; | |
} | |
- (void)dealloc | |
{ | |
//NSLog(@"UrlConnection for %@ freed",url); | |
[theConnection cancel]; | |
[theConnection release]; | |
[receivedData release]; | |
[super dealloc]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment