Little modified UIActivityIndicatorView with the ability to display activity indicator before the download data.
UIDownloadActivityIndicator.h
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
@class UIProgressView;
@protocol UIDownloadActivityIndicatorDelegate; @interface UIDownloadActivityIndicator : UIActivityIndicatorView { NSURLRequest* DownloadRequest; NSURLConnection* DownloadConnection; NSMutableData* receivedData; NSString* localFilename; id long long bytesReceived; long long expectedBytes; float percentComplete; } - (UIDownloadActivityIndicator *)initWithURL:(NSURL *)fileURL withActivityIndicatorStyle:(UIActivityIndicatorViewStyle)style timeout:(NSInteger)timeout delegate:(id - (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 @property (nonatomic, readonly) float percentComplete; @end @protocol UIDownloadActivityIndicatorDelegate @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
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
#import "UIDownloadActivityIndicator.h"
@implementation UIDownloadActivityIndicator @synthesize DownloadRequest, DownloadConnection, receivedData, delegate, percentComplete; - (UIDownloadActivityIndicator *)initWithURL:(NSURL *)fileURL withActivityIndicatorStyle:(UIActivityIndicatorViewStyle)style timeout:(NSInteger)timeout delegate:(id 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 |
Comments are closed.