1

I am able to download files from Google drive, but I want to know the download progress. Can any one tell me how to do it?

I have tried this for downloading the files:

NSString *downloadURL = [[self.driveFiles objectAtIndex:indexPath.row] downloadUrl];
GTMHTTPFetcher *fetcher = [self.driveService.fetcherService fetcherWithURLString:downloadURL];
filename=[[NSString alloc] init];
[fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error)
 {
     GTLDriveFile *file = [driveFiles objectAtIndex:indexPath.row];
     NSLog(@"\n\n\n\n\n");
     NSLog(@"This is File Size=====>%@",file.fileSize);
     NSLog(@"This is file Name===>%@",file.title);

     if(file.downloadUrl!= nil)
     {
         if (data!=nil)
         {
             filename=file.title;

             NSArray *paths =    NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
             NSString *documentsDirectory = [paths objectAtIndex:0];
             documentsDirectory = [[paths   objectAtIndex:0]stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",filename]];
             [data writeToFile:documentsDirectory atomically:YES];
nhahtdh
  • 55,989
  • 15
  • 126
  • 162
NextStep
  • 429
  • 5
  • 20
  • It's very hard to answer a question like this without providing anymore information. You could provide the code you use, and maybe a link to the API etc. – IluTov Aug 26 '13 at 12:12
  • @NSAddict plz check the update code for downloading file from google drive.. how can i know download progress when file is downloading.... – NextStep Aug 26 '13 at 12:18
  • Added my answer, hope it helps – IluTov Aug 26 '13 at 12:32
  • @NSAddict i will implement and let u know... Thanks – NextStep Aug 26 '13 at 12:36
  • @NSAddict i have declared long int for totalnumber of bytes globally ..but i m not getting the progress.. can u clear my doubt... – NextStep Aug 26 '13 at 13:12
  • @NSAddict i implemented ur suggested code.... but can u tell me where can i call this method or it will automatically called..when file start downloading..can u clear my doubt – NextStep Aug 26 '13 at 13:14
  • @NSAddict it will be appreciable..if u clear my doubt...thanks – NextStep Aug 26 '13 at 13:18
  • Does the `myFetcher:receivedData:` method get called at all? – IluTov Aug 26 '13 at 13:25
  • You have to add your instance as a delegate, so call `[fetcher beginFetchWithDelegate:self didFinishSelector:@selector(completionMethod)];`, now copy the code you had in the completion block into a method called `completionMethod`. The `myFetcher:receivedData:` method should now get called automatically – IluTov Aug 26 '13 at 13:27
  • You have my support email, what's the problem? Delete your comments, they don't belong here – IluTov Oct 03 '13 at 12:47

1 Answers1

4

Edit


I found a much simpler solution. There is a block that will be used for callbacks of the progress:

GTMHTTPFetcher *fetcher =
    [self.driveService.fetcherService fetcherWithURLString:downloadURL];
GTLDriveFile *file = [driveFiles objectAtIndex:indexPath.row];

[fetcher setReceivedDataBlock:^(NSData *data) {
    NSLog(@"%f%% Downloaded", (100.0 / [file.fileSize longLongValue] * [data length]));
}];
IluTov
  • 6,807
  • 6
  • 41
  • 103