Permalink
Browse files

UIDownloadBar project

  • Loading branch information...
0 parents commit f9b3aff9346856e2c2b8e56461663e60685ac0c8 @sakrist committed Oct 23, 2011
BIN .DS_Store
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,64 @@
+//
+// UIDownloadBar.h
+// Old Radio
+//
+// Created by Yuliya Sosnenko on 7/8/10.
+// Copyright 2010 __MyCompanyName__. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+#import <UIKit/UIKit.h>
+
+@class UIProgressView;
+@protocol UIDownloadBarDelegate;
+
+@interface UIDownloadBar : UIProgressView {
+ NSURLRequest *DownloadRequest;
+ NSURLConnection *DownloadConnection;
+ NSMutableData *receivedData;
+ NSString *localFilename;
+ NSURL *downloadUrl;
+ id<UIDownloadBarDelegate> delegate;
+ float bytesReceived;
+ long long expectedBytes;
+
+ BOOL operationFinished, operationFailed, operationBreaked;
+ BOOL operationIsOK;
+ BOOL appendIfExist;
+ FILE *downFile;
+ //NSString *fileUrlPath;
+ NSString *possibleFilename;
+
+
+ float percentComplete;
+}
+
+- (UIDownloadBar *)initWithURL:(NSURL *)fileURL progressBarFrame:(CGRect)frame timeout:(NSInteger)timeout delegate:(id<UIDownloadBarDelegate>)theDelegate;
+
+@property (assign) BOOL operationIsOK;
+@property (assign) BOOL appendIfExist;
+//@property (nonatomic, copy) NSString *fileUrlPath;
+
+@property (nonatomic, readonly) NSMutableData* receivedData;
+@property (nonatomic, readonly, retain) NSURLRequest* DownloadRequest;
+@property (nonatomic, readonly, retain) NSURLConnection* DownloadConnection;
+@property (nonatomic, assign) id<UIDownloadBarDelegate> delegate;
+
+@property (nonatomic, readonly) float percentComplete;
+@property (nonatomic, retain) NSString *possibleFilename;
+
+- (void) forceStop;
+
+- (void) forceContinue;
+
+@end
+
+
+@protocol UIDownloadBarDelegate<NSObject>
+
+@optional
+- (void)downloadBar:(UIDownloadBar *)downloadBar didFinishWithData:(NSData *)fileData suggestedFilename:(NSString *)filename;
+- (void)downloadBar:(UIDownloadBar *)downloadBar didFailWithError:(NSError *)error;
+- (void)downloadBarUpdated:(UIDownloadBar *)downloadBar;
+
+@end
@@ -0,0 +1,144 @@
+//
+// UIDownloadBar.m
+// Old Radio
+//
+// Created by Yuliya Sosnenko on 7/8/10.
+// Copyright 2010 __MyCompanyName__. All rights reserved.
+//
+
+#import "UIDownloadBar.h"
+
+
+@implementation UIDownloadBar
+
+@synthesize DownloadRequest,
+DownloadConnection,
+receivedData,
+delegate,
+percentComplete,
+operationIsOK,
+appendIfExist,
+//fileUrlPath,
+possibleFilename;
+
+- (void) forceStop {
+ operationBreaked = YES;
+}
+
+- (void) forceContinue {
+ operationBreaked = NO;
+
+ NSLog(@"%f",bytesReceived);
+ NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: downloadUrl];
+
+ [request addValue: [NSString stringWithFormat: @"bytes=%.0f-", bytesReceived ] forHTTPHeaderField: @"Range"];
+
+ DownloadConnection = [NSURLConnection connectionWithRequest:request
+ delegate: self];
+}
+
+
+- (UIDownloadBar *)initWithURL:(NSURL *)fileURL progressBarFrame:(CGRect)frame timeout:(NSInteger)timeout delegate:(id<UIDownloadBarDelegate>)theDelegate {
+ self = [super initWithFrame:frame];
+ if(self) {
+ self.delegate = theDelegate;
+ downloadUrl = fileURL;
+ bytesReceived = percentComplete = 0;
+ localFilename = [[[fileURL absoluteString] lastPathComponent] copy];
+ receivedData = [[NSMutableData alloc] initWithLength:0];
+ self.progress = 0.0;
+ 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)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
+
+ if (!operationBreaked) {
+
+ [self.receivedData appendData:data];
+
+ float receivedLen = [data length];
+ bytesReceived = (bytesReceived + receivedLen);
+
+ if(expectedBytes != NSURLResponseUnknownLength) {
+ self.progress = ((bytesReceived/(float)expectedBytes)*100)/100;
+ percentComplete = self.progress*100;
+ }
+ //NSLog(@" Data receiving... Percent complete: %f", percentComplete);
+
+ [delegate downloadBarUpdated:self];
+
+ } else {
+ [connection cancel];
+ NSLog(@" STOP !!!! Receiving data was stoped");
+ }
+
+}
+
+- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
+ [self.delegate downloadBar:self didFailWithError:error];
+ operationFailed = YES;
+ [connection release];
+}
+
+//- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
+// expectedBytes = [response expectedContentLength];
+// NSLog(@"DID RECEIVE RESPONSE");
+//}
+
+- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
+
+ NSLog(@"[DO::didReceiveData] %d operation", (int)self);
+ NSLog(@"[DO::didReceiveData] ddb: %.2f, wdb: %.2f, ratio: %.2f",
+ (float)bytesReceived,
+ (float)expectedBytes,
+ (float)bytesReceived / (float)expectedBytes);
+
+ NSHTTPURLResponse *r = (NSHTTPURLResponse*) response;
+ NSDictionary *headers = [r allHeaderFields];
+ NSLog(@"[DO::didReceiveResponse] response headers: %@", headers);
+ if (headers){
+ if ([headers objectForKey: @"Content-Range"]) {
+ NSString *contentRange = [headers objectForKey: @"Content-Range"];
+ NSLog(@"Content-Range: %@", contentRange);
+ NSRange range = [contentRange rangeOfString: @"/"];
+ NSString *totalBytesCount = [contentRange substringFromIndex: range.location + 1];
+ expectedBytes = [totalBytesCount floatValue];
+ } else if ([headers objectForKey: @"Content-Length"]) {
+ NSLog(@"Content-Length: %@", [headers objectForKey: @"Content-Length"]);
+ expectedBytes = [[headers objectForKey: @"Content-Length"] floatValue];
+ } else expectedBytes = -1;
+
+ if ([@"Identity" isEqualToString: [headers objectForKey: @"Transfer-Encoding"]]) {
+ expectedBytes = bytesReceived;
+ operationFinished = YES;
+ }
+ }
+}
+
+
+- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
+ [self.delegate downloadBar:self didFinishWithData:self.receivedData suggestedFilename:localFilename];
+ operationFinished = YES;
+ NSLog(@"Connection did finish loading...");
+ //[connection release];
+}
+
+
+- (void)dealloc {
+ [possibleFilename release];
+ [localFilename release];
+ [receivedData release];
+ [DownloadRequest release];
+// [DownloadConnection release];
+ [super dealloc];
+}
+
+@end
@@ -0,0 +1,22 @@
+//
+// UIDownloadBarAppDelegate.h
+// UIDownloadBar
+//
+// Created by Yuliya Sosnenko on 4/28/10.
+// Copyright __MyCompanyName__ 2010. All rights reserved.
+//
+
+#import <UIKit/UIKit.h>
+#import "UIDownloadBar.h"
+
+
+
+@interface UIDownloadBarAppDelegate : NSObject <UIApplicationDelegate, UIDownloadBarDelegate> {
+ UIWindow *window;
+
+ UIDownloadBar *bar;
+}
+
+@property (nonatomic, retain) IBOutlet UIWindow *window;
+
+@end
@@ -0,0 +1,68 @@
+//
+// UIDownloadBarAppDelegate.m
+// UIDownloadBar
+//
+// Created by Yuliya Sosnenko on 4/28/10.
+// Copyright __MyCompanyName__ 2010. All rights reserved.
+//
+
+#import "UIDownloadBarAppDelegate.h"
+#import "UIDownloadBar.h"
+
+@implementation UIDownloadBarAppDelegate
+
+@synthesize window;
+
+
+- (void)applicationDidFinishLaunching:(UIApplication *)application {
+
+ // Add the tab bar controller's current view as a subview of the window
+
+
+ bar = [[UIDownloadBar alloc] initWithURL:[NSURL URLWithString:@"https://dl-ssl.google.com/chrome/mac/stable/GGRM/googlechrome.dmg"]
+ progressBarFrame:CGRectMake(30, 100, 200, 20)
+ timeout:15
+ delegate:self];
+
+ [window addSubview:bar];
+ [bar release];
+
+}
+
+- (void)applicationWillTerminate:(UIApplication *)application{
+}
+
+
+
+- (void)downloadBar:(UIDownloadBar *)downloadBar didFinishWithData:(NSData *)fileData suggestedFilename:(NSString *)filename {
+ NSLog(@"%@", filename);
+}
+
+- (void)downloadBar:(UIDownloadBar *)downloadBar didFailWithError:(NSError *)error {
+ NSLog(@"%@", error);
+}
+
+- (void)downloadBarUpdated:(UIDownloadBar *)downloadBar {}
+
+
+
+/*
+// Optional UITabBarControllerDelegate method
+- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
+}
+*/
+
+/*
+// Optional UITabBarControllerDelegate method
+- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed {
+}
+*/
+
+
+- (void)dealloc {
+ [window release];
+ [super dealloc];
+}
+
+@end
+
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+ <key>CFBundleDevelopmentRegion</key>
+ <string>English</string>
+ <key>CFBundleDisplayName</key>
+ <string>${PRODUCT_NAME}</string>
+ <key>CFBundleExecutable</key>
+ <string>${EXECUTABLE_NAME}</string>
+ <key>CFBundleIconFile</key>
+ <string>Icon.png</string>
+ <key>CFBundleIdentifier</key>
+ <string>com.injoit.${PRODUCT_NAME}</string>
+ <key>CFBundleInfoDictionaryVersion</key>
+ <string>6.0</string>
+ <key>CFBundleName</key>
+ <string>${PRODUCT_NAME}</string>
+ <key>CFBundlePackageType</key>
+ <string>APPL</string>
+ <key>CFBundleSignature</key>
+ <string>????</string>
+ <key>CFBundleVersion</key>
+ <string>1.0</string>
+ <key>LSRequiresIPhoneOS</key>
+ <true/>
+ <key>UIPrerenderedIcon</key>
+ <true/>
+ <key>NSMainNibFile</key>
+ <string>MainWindow</string>
+ <key>UIStatusBarHidden</key>
+ <false/>
+</dict>
+</plist>
Oops, something went wrong.

0 comments on commit f9b3aff

Please sign in to comment.