Custom UIActivityIndicatorView

@class UIProgressView;
@protocol UIDownloadActivityIndicatorDelegate;

@interface UIDownloadActivityIndicator : UIActivityIndicatorView {
NSURLRequest
* DownloadRequest;
NSURLConnection
* DownloadConnection;
NSMutableData
* receivedData;
NSString
* localFilename;
id
<UIDownloadActivityIndicatorDelegate>delegate;
longlong bytesReceived;
longlong expectedBytes;

float percentComplete;
}

- (UIDownloadActivityIndicator *)initWithURL:(NSURL *)fileURL withActivityIndicatorStyle:(UIActivityIndicatorViewStyle)style timeout:(NSInteger)timeout delegate:(id<UIDownloadActivityIndicatorDelegate>)theDelegate;
- (void) setURL:(NSURL *)fileURL timeout:(NSInteger)timeout;


@property (nonatomic,
readonly) NSMutableData* receivedData;
@property (nonatomic,
readonly, retain) NSURLRequest* DownloadRequest;
@property (nonatomic,
readonly, retain) NSURLConnection* DownloadConnection;
@property (nonatomic, assign) id
<UIDownloadActivityIndicatorDelegate>delegate;

@property (nonatomic,
readonly) float percentComplete;

@end

@protocol UIDownloadActivityIndicatorDelegate
<NSObject>

@optional
- (void)downloadBar:(UIDownloadActivityIndicator *)downloadBar didFinishWithData:(NSData *)fileData suggestedFilename:(NSString *)filename;
- (void)downloadBar:(UIDownloadActivityIndicator *)downloadBar didFailWithError:(NSError *)error;
- (void)downloadBarUpdated:(UIDownloadActivityIndicator *)downloadBar;

@end

 

UIDownloadActivityIndicator.h

 

#import "UIDownloadActivityIndicator.h"

@implementation UIDownloadActivityIndicator

@synthesize DownloadRequest,
DownloadConnection,
receivedData,
delegate,
percentComplete;


- (UIDownloadActivityIndicator *)initWithURL:(NSURL *)fileURL withActivityIndicatorStyle:(UIActivityIndicatorViewStyle)style timeout:(NSInteger)timeout delegate:(id<UIDownloadActivityIndicatorDelegate>)theDelegate {
self
= [super initWithActivityIndicatorStyle:style];
if(self) {
self.
delegate= theDelegate;
bytesReceived
= percentComplete =0;
localFilename
= [[[fileURL absoluteString] lastPathComponent] copy];
receivedData
= [[NSMutableData alloc] initWithLength:0];
//self.progress = 0.0;
[self startAnimating];
self.backgroundColor
= [UIColor clearColor];
DownloadRequest
= [[NSURLRequest alloc] initWithURL:fileURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:timeout];
DownloadConnection
= [[NSURLConnection alloc] initWithRequest:DownloadRequest delegate:self startImmediately:YES];

if(DownloadConnection == nil) {
[self.
delegate downloadBar:self
didFailWithError:[NSError errorWithDomain:
@"UIDownloadBar Error"
code:
1
userInfo:[NSDictionary
dictionaryWithObjectsAndKeys:
@"NSURLConnection Failed",
NSLocalizedDescriptionKey, nil]]];
}
}

return self;
}

- (void) setURL:(NSURL *)fileURL timeout:(NSInteger)timeout {
[receivedData release];
[DownloadRequest release];


bytesReceived
= percentComplete =0;
localFilename
= [[[fileURL absoluteString] lastPathComponent] copy];

receivedData
= [[NSMutableData alloc] initWithLength:0];

DownloadRequest
= [[NSURLRequest alloc] initWithURL:fileURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:timeout];
DownloadConnection
= [[NSURLConnection alloc] initWithRequest:DownloadRequest delegate:self startImmediately:YES];

if(DownloadConnection == nil) {
[self.
delegate downloadBar:self
didFailWithError:[NSError errorWithDomain:
@"UIDownloadBar Error"
code:
1
userInfo:[NSDictionary
dictionaryWithObjectsAndKeys:
@"NSURLConnection Failed",
NSLocalizedDescriptionKey, nil]]];
}
}


- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.receivedData appendData:data];

NSInteger receivedLen
= [data length];
bytesReceived
= (bytesReceived + receivedLen);

if(expectedBytes != NSURLResponseUnknownLength) {
//self.progress = ((bytesReceived/(float)expectedBytes)*100)/100;
//percentComplete = self.progress*100;
}

[
delegate downloadBarUpdated:self];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[self.
delegate downloadBar:self didFailWithError:error];
[connection release];
connection
= nil;
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
expectedBytes
= [response expectedContentLength];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[self.
delegate downloadBar:self didFinishWithData:self.receivedData suggestedFilename:localFilename];
[connection release];
connection
= nil;
}

- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
}

- (void)dealloc {
[localFilename release];
[receivedData release];
[DownloadRequest release];
if (DownloadConnection != nil ) {
[DownloadConnection release];
DownloadConnection
= nil;
}
[super dealloc];
}

@end

 

posted @ 2011-03-16 16:00  jprothwell  阅读(409)  评论(0编辑  收藏  举报