0

I am downloading video file from response. i want to display downloading progress bar of HUD-progress. but how can i do that. I am sending verify json to server and server verify that send back the video file bytes. i want to display how much percentage of downloading is done by using HUD-progreasse bar.

If i call [request setDidReceiveDataSelector:@selector(request:didReceiveBytes:)]; than it display how much bytes i got but it doesn't store the bytes into cache file ( it doesn't not store file into phone)

  ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[[NSURL alloc] initWithString:@"http://testing.io/dev.php/video/verifyReceipt"]];
  [request setPostValue:resultAsString forKey:@"verify"];// sending json in post 
[request setDidReceiveDataSelector:@selector(request:didReceiveBytes:)]; 
[request setDidFinishSelector:@selector(requestDone:)];
[request setTimeOutSeconds:120];
[request setDelegate:self];
[request setNumberOfTimesToRetryOnTimeout:2];
[request setDownloadProgressDelegate:self];
request.showAccurateProgress = YES;

[request startSynchronous];

 }
 -(void)request:(ASIHTTPRequest *)request didReceiveData:(NSData *)data
 {
[videoData appendData:data];// appending data with global NSmutabledata
  NSLog(@"data is %@",data);
   }

- (void)requestDone:(ASIHTTPRequest *)request{
//[MBProgressHUD hideHUDForView:self.view animated:YES];

// SAVED Video PATH
// Get the Document directory
NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
// Add your filename to the directory to create your saved video location
NSString* movLocation = [documentDirectory stringByAppendingPathComponent:[fileName stringByAppendingString:@".mov"]];

if(request.responseStatusCode==200)
{
    [videoData writeToFile:movLocation atomically:NO];
    NSLog(@"in request done sucsessfully downlaod and store in database %d",request.responseStatusCode);
    [DBHelper savePurchaseId:fileName];
    [self movieReceived];
}
else{

      NSLog(@"in request downlaod and store in database failed %@",request.responseHeaders);

}
 }
     -(void)requestFailed:(ASIHTTPRequest *)request
      {

NSLog(@"%@",request.error);

      }
Swap-IOS-Android
  • 4,363
  • 6
  • 49
  • 77

2 Answers2

0

fileUrl = string of URL to download a video.

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[fileUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

    operation.outputStream = [NSOutputStream outputStreamToFileAtPath:destPath append:NO];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
    {
        // Give alert that downloading successful.
        NSLog(@"Successfully downloaded file to %@", destPath);

        /* To call delegate to response the result. */
        [self.target parserDidDownloadItem:destPath]; 

        HUD.detailsLabelText = [NSString stringWithFormat:@"%@ %i%%",[JGlobals getLocalvalue:@"Downloading"],100];
        [HUD hide:TRUE];
    }
    failure:^(AFHTTPRequestOperation *operation, NSError *error)
    {
        // Give alert that downloading failed
        NSLog(@"Error: %@", error);

        /* To call delegate to response the result. */
        [self.target parserDidFailToDownloadItem:error];
        [HUD hide:TRUE];

    }];
    [operation setDownloadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite)
    {
         // Progress
         float totalProgress = (totalBytesWritten / (totalBytesExpectedToWrite * 1.0f) * 100);
         HUD.detailsLabelText = [NSString stringWithFormat:@"Downloading %i%%", MIN((int)(totalProgress), 99)];
     }];
    [operation start];
alloc_iNit
  • 5,173
  • 2
  • 26
  • 54
0

I'm thinking of a method which might not be the right one, but it going to save you from writing a lot of code.

First set a normal UIProgressView instance as the download progress delegate like this

[request setDownloadProgressDelegate:uiprogreeViewInstance];

Now ASIHTTPRequest framework will take care of updating that progressview when the downloading will be taking place. ProgressView has a property named progress, which ranges from 0.0 to 1.0.

Now instantiate MBProgressHUD, add it as your subview where you want. Set the progress value of the MBProgressHUD instance to that of the UIProgressView. Hide the uiprogressview instance.

Adithya
  • 4,545
  • 3
  • 25
  • 28