iphone - MBHUD进度条加ASIHTTPRequest用于跟踪下载

标签 iphone ios

我正在使用 ASIHttpRequest 和 MB-HUD-progress 库,现在我想显示 MBHUD 进度条以下载视频文件。 我混淆了这两个库的文档。它们对初学者来说还不够。

这是我在应用内购买库中使用的代码。我不知道你如何实现下面的 ASSiHttpRequest 方法来使用这两个库进行下载

- (void) completeTransaction: (SKPaymentTransaction *)transaction
 {
NSLog(@"Transaction Completed");
// You can create a method to record the transaction.
// [self recordTransaction: transaction];

// You should make the update to your app based on what was purchased and inform user.
// [self provideContent: transaction.payment.productIdentifier];

// Finally, remove the transaction from the payment queue.

HUD = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:HUD];

// Set determinate mode
HUD.mode = MBProgressHUDModeAnnularDeterminate;
//HUD.delegate = self;
HUD.labelText = @"Downloading";

// myProgressTask uses the HUD instance to update progress
[HUD showWhileExecuting:@selector(myProgressTask) onTarget:self withObject:nil animated:YES];

[self downloadFromURL:[NSURL URLWithString:@"http://techslides.com/demos/sample-videos/small.mp4"]];
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
    }

   -(void)downloadFromURL:(NSURL *)url
     {

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

   ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
  filePath = [documentsDirectory stringByAppendingPathComponent:@"small.mp4"];
  NSLog(@"Path is %@",filePath);
  [request setDelegate:self];
  [request setTimeOutSeconds:60];
  [request setNumberOfTimesToRetryOnTimeout:2];
  [request setDownloadDestinationPath:filePath];
  [request startAsynchronous];
  [request setDownloadProgressDelegate:self];
   request.showAccurateProgress = YES;
  }


    - (void)request:(ASIHTTPRequest *)request incrementDownloadSizeBy:(long long)newLength
   {
    NSLog(@"newLength: %lld", newLength);
     }

    -(void)request:(ASIHTTPRequest *)request didReceiveBytes:(long long)bytes {
    NSLog(@"Received bytes:%lld",bytes);

       }

     -(void)requestFinished:(ASIHTTPRequest *)request

   {
 [MBProgressHUD hideHUDForView:self.view animated:YES];
 [self movieReceived];

    }

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

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

    }


   -(void)movieReceived
      {
    playerController = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];
     }


 - (void)myProgressTask {
// This just increases the progress indicator in a loop
float progress = 0.0f;
while (progress < 1.0f) {
    progress += 0.01f;
    HUD.progress = progress;
    usleep(60000);
}
     }

我不知道如何更改 myprogresstask 的所有值以根据文件大小更新进度条..

感谢任何帮助。

最佳答案

计算接收到的字节数占总字节数的百分比

-(void)request:(ASIHTTPRequest *)request didReceiveBytes:(long long)bytes

并设置进度值

HUD.progress = normalisedPercentage;

关于iphone - MBHUD进度条加ASIHTTPRequest用于跟踪下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16813052/

相关文章:

iphone - 振荡 View 动画

iphone - 如何获取关系键

iPhone 接口(interface)尺寸

ios - MacinCloud 代理上的 VSTS Cordova iOS 构建失败 - "Cannot read property ' 未定义失败”

ios - TLIndexPathTools - 动态加载树 TableView ?

iphone - 加载应用程序时 NSUserDefaults 发生了一些奇怪的事情

iphone - iOS - UIButton 的文本在按下时被设置回 Nib 设置值

ios - 不希望导航和选项卡出现在初始 ViewController 上

ios - iOS 的 kAudioDevicePropertyBufferFrameSize 替换

iphone - 如何使用 OpenGL ES 2.0 应用光源?