Custom UIActivityIndicatorView
Categories: iPhone on Oct.29, 2009
Little modified UIActivityIndicatorView with the ability to display activity indicator before the download data.
UIDownloadActivityIndicator.h
- @class UIProgressView;
- @protocol UIDownloadActivityIndicatorDelegate;
- @interface UIDownloadActivityIndicator : UIActivityIndicatorView {
- NSURLRequest* DownloadRequest;
- NSURLConnection* DownloadConnection;
- NSMutableData* receivedData;
- NSString* localFilename;
- id<UIDownloadActivityIndicatorDelegate> delegate;
- long long bytesReceived;
- long long 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.m
- #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
Similar posts:



December 14th, 2009 on 8:44 am
Еще бы немного кода – как использовать этот прогресс бар
- надо добавлять его как addSubview или нет?
- что представляет собой suggestedFileName?
- что писать в downloadBarUpdated?
December 15th, 2009 on 12:03 pm
- надо добавлять его как addSubview или нет?
да
- что представляет собой suggestedFileName?
имя скаченного файла
- что писать в downloadBarUpdated?
можно ничего не писать :)