Little modified UIActivityIndicatorView with the ability to display activity indicator before the download data.

UIDownloadActivityIndicator.h

  1. @class UIProgressView;
  2. @protocol UIDownloadActivityIndicatorDelegate;
  3.  
  4. @interface UIDownloadActivityIndicator : UIActivityIndicatorView {
  5.     NSURLRequest* DownloadRequest;
  6.     NSURLConnection* DownloadConnection;
  7.     NSMutableData* receivedData;
  8.     NSString* localFilename;
  9.     id<UIDownloadActivityIndicatorDelegate> delegate;
  10.     long long bytesReceived;
  11.     long long expectedBytes;
  12.    
  13.     float percentComplete;
  14. }
  15.  
  16. - (UIDownloadActivityIndicator *)initWithURL:(NSURL *)fileURL withActivityIndicatorStyle:(UIActivityIndicatorViewStyle)style timeout:(NSInteger)timeout delegate:(id<UIDownloadActivityIndicatorDelegate>)theDelegate;
  17. - (void) setURL:(NSURL *)fileURL timeout:(NSInteger)timeout;
  18.  
  19.  
  20. @property (nonatomic, readonly) NSMutableData* receivedData;
  21. @property (nonatomic, readonly, retain) NSURLRequest* DownloadRequest;
  22. @property (nonatomic, readonly, retain) NSURLConnection* DownloadConnection;
  23. @property (nonatomic, assign) id<UIDownloadActivityIndicatorDelegate> delegate;
  24.  
  25. @property (nonatomic, readonly) float percentComplete;
  26.  
  27. @end
  28.  
  29. @protocol UIDownloadActivityIndicatorDelegate<NSObject>
  30.  
  31. @optional
  32. - (void)downloadBar:(UIDownloadActivityIndicator *)downloadBar didFinishWithData:(NSData *)fileData suggestedFilename:(NSString *)filename;
  33. - (void)downloadBar:(UIDownloadActivityIndicator *)downloadBar didFailWithError:(NSError *)error;
  34. - (void)downloadBarUpdated:(UIDownloadActivityIndicator *)downloadBar;
  35.  
  36. @end


UIDownloadActivityIndicator.m

  1. #import "UIDownloadActivityIndicator.h"
  2.  
  3. @implementation UIDownloadActivityIndicator
  4.  
  5. @synthesize DownloadRequest,
  6. DownloadConnection,
  7. receivedData,
  8. delegate,
  9. percentComplete;
  10.  
  11.  
  12. - (UIDownloadActivityIndicator *)initWithURL:(NSURL *)fileURL withActivityIndicatorStyle:(UIActivityIndicatorViewStyle)style timeout:(NSInteger)timeout delegate:(id<UIDownloadActivityIndicatorDelegate>)theDelegate {
  13.     self = [super initWithActivityIndicatorStyle:style];
  14.     if(self) {
  15.         self.delegate = theDelegate;
  16.         bytesReceived = percentComplete = 0;
  17.         localFilename = [[[fileURL absoluteString] lastPathComponent] copy];
  18.         receivedData = [[NSMutableData alloc] initWithLength:0];
  19.         //self.progress = 0.0;
  20.         [self startAnimating];
  21.         self.backgroundColor = [UIColor clearColor];
  22.         DownloadRequest = [[NSURLRequest alloc] initWithURL:fileURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:timeout];
  23.         DownloadConnection = [[NSURLConnection alloc] initWithRequest:DownloadRequest delegate:self startImmediately:YES];
  24.        
  25.         if(DownloadConnection == nil) {
  26.             [self.delegate downloadBar:self
  27.                       didFailWithError:[NSError errorWithDomain:@"UIDownloadBar Error"
  28.                                                            code:1
  29.                                                        userInfo:[NSDictionary
  30.                                                                  dictionaryWithObjectsAndKeys:@"NSURLConnection Failed",
  31.                                                                  NSLocalizedDescriptionKey, nil]]];
  32.         }
  33.     }
  34.    
  35.     return self;
  36. }
  37.  
  38. - (void) setURL:(NSURL *)fileURL timeout:(NSInteger)timeout {
  39.     [receivedData release];
  40.     [DownloadRequest release];
  41.    
  42.    
  43.     bytesReceived = percentComplete = 0;
  44.     localFilename = [[[fileURL absoluteString] lastPathComponent] copy];
  45.    
  46.     receivedData = [[NSMutableData alloc] initWithLength:0];
  47.    
  48.     DownloadRequest = [[NSURLRequest alloc] initWithURL:fileURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:timeout];
  49.     DownloadConnection = [[NSURLConnection alloc] initWithRequest:DownloadRequest delegate:self startImmediately:YES];
  50.    
  51.     if(DownloadConnection == nil) {
  52.         [self.delegate downloadBar:self
  53.                   didFailWithError:[NSError errorWithDomain:@"UIDownloadBar Error"
  54.                                                        code:1
  55.                                                    userInfo:[NSDictionary
  56.                                                              dictionaryWithObjectsAndKeys:@"NSURLConnection Failed",
  57.                                                              NSLocalizedDescriptionKey, nil]]];
  58.     }
  59. }
  60.  
  61.  
  62. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
  63.     [self.receivedData appendData:data];
  64.    
  65.     NSInteger receivedLen = [data length];
  66.     bytesReceived = (bytesReceived + receivedLen);
  67.    
  68.     if(expectedBytes != NSURLResponseUnknownLength) {
  69.         //self.progress = ((bytesReceived/(float)expectedBytes)*100)/100;
  70.         //percentComplete = self.progress*100;
  71.     }
  72.    
  73.     [delegate downloadBarUpdated:self];
  74. }
  75.  
  76. - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
  77.     [self.delegate downloadBar:self didFailWithError:error];
  78.     [connection release];
  79.     connection = nil;
  80. }
  81.  
  82. - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
  83.     expectedBytes = [response expectedContentLength];
  84. }
  85.  
  86. - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
  87.     [self.delegate downloadBar:self didFinishWithData:self.receivedData suggestedFilename:localFilename];
  88.     [connection release];
  89.     connection = nil;
  90. }
  91.  
  92. - (void)drawRect:(CGRect)rect {
  93.     [super drawRect:rect];
  94. }
  95.  
  96. - (void)dealloc {
  97.     [localFilename release];
  98.     [receivedData release];
  99.     [DownloadRequest release];
  100.     if (DownloadConnection != nil ) {
  101.         [DownloadConnection release];
  102.         DownloadConnection = nil;
  103.     }
  104.     [super dealloc];
  105. }
  106.  
  107. @end