Giter Club home page Giter Club logo

simple-download-manager's Introduction

This download manager uses the ASIHTTPRequest classes to download files. Can download files in back ground. Can download multiple files once at a time. It can resume interrupted downloads.

USAGE: You need ASIHTTPRequest classes get it https://github.com/pokeb/asi-http-request and setup your project Copy the downloadcell and downloadtableviewcontroller in your project.

Simply in your view controller viewdidload method do:

This will check for any interrupted download and resume it

downloadTableViewObj = [[DownloadTableViewController alloc] init];
[downloadTableViewObj setDelegate:self];
[downloadTableViewObj getInterruptedDownloadsAndResume];

And populate url in an array like this:

urlArray = [NSMutableArray arrayWithObjects:
@"http://dl.dropbox.com/u/97700329/file1.mp4",
@"http://dl.dropbox.com/u/97700329/file2.mp4",
@"http://dl.dropbox.com/u/97700329/file3.mp4",nil];

Remove the urls from your array that was interrupted because if you download one file multiple times it will cause problem

NSMutableArray *interruptedRequests = [[NSUserDefaults standardUserDefaults] objectForKey:@"interruptedDownloads"];
for(NSString *str in interruptedRequests)
{
  if([urlArray containsObject:str])
  [urlArray removeObject:str];
}
[myTableView reloadData];

Set up table view in your view controller

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return urlArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  NSString *cellIdentifier = [NSString stringWithFormat:@"Cell-%d-%d-%d",indexPath.section,indexPath.row,urlArray.count];
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
  if(cell == Nil)
  {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    [cell.textLabel setText:[[[urlArray objectAtIndex:indexPath.row] componentsSeparatedByString:@"/"] lastObject]];

    UIButton *downloadButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [downloadButton setFrame:CGRectMake(230, 5, 80, 35)];
    [downloadButton setTitle:@"Download" forState:UIControlStateNormal];
    [downloadButton addTarget:self action:@selector(downloadButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    [downloadButton setTag:indexPath.row];
    [cell addSubview:downloadButton];
  }
    return cell;
}

Create a request and call the method "addDownloadRequest" of DownloadTableViewController Set the file destination path

-(void)downloadButtonTapped:(UIButton *)sender
{
  NSURL *url = [NSURL URLWithString:[urlArray objectAtIndex:sender.tag]];
  ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:url];
  NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
  [request setUserInfo:dictionary];
  [downloadTableViewObj setDownloadDirectory:fileDestination];
  [downloadTableViewObj addDownloadRequest:request];
  [urlArray removeObjectAtIndex:sender.tag];
  [myTableView reloadData];
}

Use three delegate methods

-(void)downloadController:(DownloadTableViewController *)vc startedDownloadingRequest:(ASIHTTPRequest *)request
{
  NSLog(@"download started %@",[request userInfo]);
}
-(void)downloadController:(DownloadTableViewController *)vc finishedDownloadingReqeust:(ASIHTTPRequest *)request
{
  NSLog(@"download finished %@",[request userInfo]);
}
-(void)downloadController:(DownloadTableViewController *)vc failedDownloadingReqeust:(ASIHTTPRequest *)request
{
  NSLog(@"Error %@",[request error]);
  [urlArray addObject:[request.url absoluteString]];
  [myTableView reloadData];

  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Network error" message:[[request error] localizedDescription] delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
  [alert show];
}

That's it

simple-download-manager's People

Contributors

mzeeshanid avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.