-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from albertschulz/master
Downloader State, Progress Property, Writable Filename & Multiple Downloads Example
- Loading branch information
Showing
11 changed files
with
382 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,8 @@ | |
21D06E13181847F400CAADCD /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 21D06E0A181847F400CAADCD /* ViewController.m */; }; | ||
21D06E14181847F400CAADCD /* ViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 21D06E0B181847F400CAADCD /* ViewController.xib */; }; | ||
C317CBD218CB668500160ED6 /* libTCBlobDownload.a in Frameworks */ = {isa = PBXBuildFile; fileRef = C33448CA18C000850050C1ED /* libTCBlobDownload.a */; }; | ||
E17B6757190CEBE100B81CD8 /* MultipleViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = E17B6755190CEBE100B81CD8 /* MultipleViewController.m */; }; | ||
E17B6758190CEBE100B81CD8 /* MultipleViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = E17B6756190CEBE100B81CD8 /* MultipleViewController.xib */; }; | ||
/* End PBXBuildFile section */ | ||
|
||
/* Begin PBXContainerItemProxy section */ | ||
|
@@ -63,6 +65,9 @@ | |
21D06E0A181847F400CAADCD /* ViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = "<group>"; }; | ||
21D06E0B181847F400CAADCD /* ViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = ViewController.xib; sourceTree = "<group>"; }; | ||
C33448C318C000850050C1ED /* TCBlobDownload.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = TCBlobDownload.xcodeproj; path = ../TCBlobDownload/TCBlobDownload.xcodeproj; sourceTree = "<group>"; }; | ||
E17B6754190CEBE100B81CD8 /* MultipleViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MultipleViewController.h; sourceTree = "<group>"; }; | ||
E17B6755190CEBE100B81CD8 /* MultipleViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MultipleViewController.m; sourceTree = "<group>"; }; | ||
E17B6756190CEBE100B81CD8 /* MultipleViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = MultipleViewController.xib; sourceTree = "<group>"; }; | ||
/* End PBXFileReference section */ | ||
|
||
/* Begin PBXFrameworksBuildPhase section */ | ||
|
@@ -116,6 +121,9 @@ | |
21D06E09181847F400CAADCD /* ViewController.h */, | ||
21D06E0A181847F400CAADCD /* ViewController.m */, | ||
21D06E0B181847F400CAADCD /* ViewController.xib */, | ||
E17B6754190CEBE100B81CD8 /* MultipleViewController.h */, | ||
E17B6755190CEBE100B81CD8 /* MultipleViewController.m */, | ||
E17B6756190CEBE100B81CD8 /* MultipleViewController.xib */, | ||
21D06E15181847FF00CAADCD /* Ressources */, | ||
21D06E161818480A00CAADCD /* Supporting Files */, | ||
); | ||
|
@@ -236,6 +244,7 @@ | |
21D06E10181847F400CAADCD /* InfoPlist.strings in Resources */, | ||
21D06E0D181847F400CAADCD /* [email protected] in Resources */, | ||
21D06E0E181847F400CAADCD /* Default.png in Resources */, | ||
E17B6758190CEBE100B81CD8 /* MultipleViewController.xib in Resources */, | ||
21D06E14181847F400CAADCD /* ViewController.xib in Resources */, | ||
); | ||
runOnlyForDeploymentPostprocessing = 0; | ||
|
@@ -249,6 +258,7 @@ | |
files = ( | ||
21D06E13181847F400CAADCD /* ViewController.m in Sources */, | ||
21D06E11181847F400CAADCD /* main.m in Sources */, | ||
E17B6757190CEBE100B81CD8 /* MultipleViewController.m in Sources */, | ||
21D06E0C181847F400CAADCD /* AppDelegate.m in Sources */, | ||
); | ||
runOnlyForDeploymentPostprocessing = 0; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// | ||
// MultipleViewController.h | ||
// TCBlobDownloadExample | ||
// | ||
// Created by Albert on 27.04.14. | ||
// Copyright (c) 2014 thibaultCha. All rights reserved. | ||
// | ||
|
||
#import <UIKit/UIKit.h> | ||
#import <TCBlobDownload/TCBlobDownload.h> | ||
|
||
@interface MultipleViewController : UITableViewController <UIAlertViewDelegate, TCBlobDownloaderDelegate> | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,204 @@ | ||
// | ||
// MultipleViewController.m | ||
// TCBlobDownloadExample | ||
// | ||
// Created by Albert on 27.04.14. | ||
// Copyright (c) 2014 thibaultCha. All rights reserved. | ||
// | ||
|
||
#import "MultipleViewController.h" | ||
|
||
#define kDownloadPath [NSString pathWithComponents:@[NSTemporaryDirectory(), @"multipleExample"]] | ||
|
||
@interface MultipleViewController () { | ||
NSMutableArray *downloads; | ||
} | ||
|
||
@end | ||
|
||
@implementation MultipleViewController | ||
|
||
- (void)viewDidLoad | ||
{ | ||
[super viewDidLoad]; | ||
|
||
downloads = [NSMutableArray new]; | ||
|
||
self.title = @"Multiple Downloads Table"; | ||
|
||
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop target:self action:@selector(dismiss)]; | ||
|
||
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(showAddDownloadAlert)]; | ||
} | ||
|
||
- (void)dismiss | ||
{ | ||
[[TCBlobDownloadManager sharedInstance] cancelAllDownloadsAndRemoveFiles:YES]; | ||
|
||
[self dismissViewControllerAnimated:YES completion:nil]; | ||
} | ||
|
||
- (void)showAddDownloadAlert | ||
{ | ||
UIAlertView *addAlertView = [[UIAlertView alloc] initWithTitle:@"Add Download" message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Add this URL", @"~ Multiple Test Downloads ~", nil]; | ||
|
||
addAlertView.alertViewStyle = UIAlertViewStylePlainTextInput; | ||
|
||
UITextField *textField = [addAlertView textFieldAtIndex:0]; | ||
textField.placeholder = @"http://"; | ||
|
||
[addAlertView show]; | ||
} | ||
|
||
- (void)addDownload:(NSURL *)url | ||
{ | ||
TCBlobDownloader *download = [[TCBlobDownloader alloc] initWithURL:url downloadPath:kDownloadPath delegate:self]; | ||
|
||
download.fileName = [url.absoluteString lastPathComponent]; | ||
|
||
[[TCBlobDownloadManager sharedInstance] startDownload:download]; | ||
|
||
[downloads addObject:download]; | ||
|
||
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic]; | ||
} | ||
|
||
#pragma mark - Helper Methods | ||
|
||
- (NSString *)subtitleForDownload:(TCBlobDownloader *)download | ||
{ | ||
NSString *stateString; | ||
|
||
switch (download.state) { | ||
case TCBlobDownloadStateReady: | ||
stateString = @"Ready"; | ||
break; | ||
case TCBlobDownloadStateDownloading: | ||
stateString = @"Downloading"; | ||
break; | ||
case TCBlobDownloadStateDone: | ||
stateString = @"Done"; | ||
break; | ||
case TCBlobDownloadStateCancelled: | ||
stateString = @"Cancelled"; | ||
break; | ||
case TCBlobDownloadStateFailed: | ||
stateString = @"Failed"; | ||
break; | ||
default: | ||
break; | ||
} | ||
|
||
return [NSString stringWithFormat:@"%i%% • %lis left • State: %@", (int)(download.progress * 100), (long)download.remainingTime, stateString]; | ||
} | ||
|
||
#pragma mark - Alert view delegate | ||
|
||
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex | ||
{ | ||
if (buttonIndex != alertView.cancelButtonIndex) { | ||
|
||
if (buttonIndex == 0) { | ||
NSString *urlString = [alertView textFieldAtIndex:0].text; | ||
|
||
[self addDownload:[NSURL URLWithString:urlString]]; | ||
} | ||
else { | ||
[self addDownload:[NSURL URLWithString:@"http://api.soundcloud.com/tracks/136369443/stream?client_id=b45b1aa10f1ac2941910a7f0d10f8e28"]]; | ||
[self addDownload:[NSURL URLWithString:@"http://api.soundcloud.com/tracks/130355303/stream?client_id=b45b1aa10f1ac2941910a7f0d10f8e28"]]; | ||
[self addDownload:[NSURL URLWithString:@"https://api.soundcloud.com/tracks/126240832/download?client_id=b45b1aa10f1ac2941910a7f0d10f8e28"]]; | ||
} | ||
|
||
} | ||
} | ||
|
||
#pragma mark - Blob downloader delegate | ||
|
||
- (void)download:(TCBlobDownloader *)blobDownload didFinishWithSuccess:(BOOL)downloadFinished atPath:(NSString *)pathToFile | ||
{ | ||
NSInteger index = [downloads indexOfObject:blobDownload]; | ||
|
||
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]]; | ||
cell.detailTextLabel.text = [self subtitleForDownload:blobDownload]; | ||
} | ||
|
||
- (void)download:(TCBlobDownloader *)blobDownload didReceiveData:(uint64_t)receivedLength onTotal:(uint64_t)totalLength progress:(float)progress | ||
{ | ||
NSInteger index = [downloads indexOfObject:blobDownload]; | ||
|
||
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]]; | ||
cell.detailTextLabel.text = [self subtitleForDownload:blobDownload]; | ||
} | ||
|
||
- (void)download:(TCBlobDownloader *)blobDownload didReceiveFirstResponse:(NSURLResponse *)response | ||
{ | ||
NSInteger index = [downloads indexOfObject:blobDownload]; | ||
|
||
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]]; | ||
cell.detailTextLabel.text = [self subtitleForDownload:blobDownload]; | ||
} | ||
|
||
- (void)download:(TCBlobDownloader *)blobDownload didStopWithError:(NSError *)error | ||
{ | ||
NSInteger index = [downloads indexOfObject:blobDownload]; | ||
|
||
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]]; | ||
cell.detailTextLabel.text = [self subtitleForDownload:blobDownload]; | ||
} | ||
|
||
#pragma mark - Table view data source | ||
|
||
|
||
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section | ||
{ | ||
// Return the number of rows in the section. | ||
return downloads.count; | ||
} | ||
|
||
|
||
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath | ||
{ | ||
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; | ||
if (!cell) { | ||
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"]; | ||
} | ||
|
||
TCBlobDownloader *download = downloads[indexPath.row]; | ||
|
||
cell.textLabel.text = download.fileName; | ||
|
||
cell.detailTextLabel.text = [self subtitleForDownload:download]; | ||
|
||
return cell; | ||
} | ||
|
||
|
||
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath | ||
{ | ||
return YES; | ||
} | ||
|
||
|
||
// Override to support editing the table view. | ||
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath | ||
{ | ||
if (editingStyle == UITableViewCellEditingStyleDelete) { | ||
|
||
TCBlobDownloader *download = downloads[indexPath.row]; | ||
[download cancelDownloadAndRemoveFile:YES]; | ||
|
||
NSInteger index = [downloads indexOfObject:download]; | ||
|
||
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]]; | ||
cell.detailTextLabel.text = [self subtitleForDownload:download]; | ||
|
||
[cell setEditing:NO animated:YES]; | ||
} | ||
} | ||
|
||
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath | ||
{ | ||
return @"Cancel"; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="no"?> | ||
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="5053" systemVersion="13C1021" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES"> | ||
<dependencies> | ||
<deployment defaultVersion="1536" identifier="iOS"/> | ||
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="3733"/> | ||
</dependencies> | ||
<objects> | ||
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="MultipleViewController"> | ||
<connections> | ||
<outlet property="view" destination="i5M-Pr-FkT" id="sfx-zR-JGt"/> | ||
</connections> | ||
</placeholder> | ||
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/> | ||
<tableView opaque="NO" clipsSubviews="YES" clearsContextBeforeDrawing="NO" contentMode="scaleToFill" bouncesZoom="NO" style="plain" separatorStyle="default" rowHeight="44" sectionHeaderHeight="22" sectionFooterHeight="22" id="i5M-Pr-FkT"> | ||
<rect key="frame" x="0.0" y="0.0" width="320" height="568"/> | ||
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/> | ||
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/> | ||
<simulatedStatusBarMetrics key="simulatedStatusBarMetrics"/> | ||
<simulatedNavigationBarMetrics key="simulatedTopBarMetrics" prompted="NO"/> | ||
<simulatedScreenMetrics key="simulatedDestinationMetrics" type="retina4"/> | ||
<connections> | ||
<outlet property="dataSource" destination="-1" id="Tng-2m-Rnh"/> | ||
<outlet property="delegate" destination="-1" id="9aC-8N-iBw"/> | ||
</connections> | ||
</tableView> | ||
</objects> | ||
</document> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.