如何使用 AFHTTPRequestOperation 更新 MBProgressHud

发布于 2025-01-06 22:47:58 字数 625 浏览 2 评论 0原文

为了学习Ios,我下载了一些例子。

示例包括:

  • AFNetworking;
  • 应用内购买(来自 RayW);
  • MBProgressHud;

在我的视图控制器中,我按下一个 UI 按钮,该按钮会触发应用内购买单例示例,并开始使用 AFHTTPRequestOperation 从我的服务器下载文件。这部分的沟通是有效的。但我想要实现的是在下载时更新我的​​HUD。由于文件>10Mb。

那么,问题是如何根据下载进度更新HUD?我试着把它画下来。

  • 我按下 Viewcontroller 中的按钮,HUD 将显示;

    • -->请求将发送到处理网络部分的 Singleton InApp 帮助程序类;

      • -->之后,将在单例类中调用 AFHTTPRequestOperation 来下载文件;

        • --->在此下载过程中,我使用 setDownloadProgressBlock 方法来获取进度。

但是我如何将进度信息发送回视图控制器中的HUD?

谢谢。

I downloaded some examples in order to learn Ios.

The examples include:

  • AFNetworking;
  • Inapp purchase (from RayW);
  • MBProgressHud;

In my viewcontroller i push a UIbutton which triggers the Inapp purchase Singleton example and start download a file from my server with AFHTTPRequestOperation. This part of communication works. But what i want to achieve is to have my hud updated while downloading. As the file is >10Mb.

So, the question is how do i update the hud with the progress of the download? I try to draw it down.

  • I push the button in Viewcontroller and the hud will displayed;

    • --> request will sent to the Singleton InApp helper class which handles the networking part;

      • --> After that the AFHTTPRequestOperation will be called inside the singleton class for the download of file;

        • ---> During this download i use the setDownloadProgressBlock method for the progress.

But how do i sent the progress info back to my hud in the viewcontroller?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谁的新欢旧爱 2025-01-13 22:47:58

这就是我按照 @mattt 建议对类似问题所做的处理。
我的 Singleton InApp helper 具有 productDownloadURL ivar 和一个 prepareForDownload 方法,该方法将 AFHTTPRequestOperation 返回给调用者:

- (AFHTTPRequestOperation * )prepareForDownload:(NSString *)productIdentifier
{
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:_productDownloadURL]];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:productIdentifier];
    operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];

    return  operation;
}

我的 >RootViewController 使用 AFHTTPRequestOperation 发出请求并设置downloadProgress/success/failure 块如下:

AFHTTPRequestOperation *operation = [[InAppRageIAPHelper sharedHelper] prepareForDownload:productIdentifier];
[operation setDownloadProgressBlock:^(NSInteger bytesRead, NSInteger totalBytesRead, NSInteger totalBytesExpectedToRead) {
     float percentDone = ((float)((int)totalBytesRead) / (float)((int)totalBytesExpectedToRead));    
     [(UIProgressView *)_hud.customView setProgress:percentDone];
     _hud.labelText = [NSString stringWithFormat:@"%f",percentDone];
 }];

 [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
   _hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"success.png"]];
   [self performSelector:@selector(dismissHUD:) withObject:nil afterDelay:1.5];
 } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
   _hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"error.png"]];
 }];
 [operation start];

hud 是一个 MBProgressHUD。您还可以使用 MBProgressHUDModeDeterminate 模式增强进度显示。

This is what I did with a similar problem, following @mattt advice.
My Singleton InApp helper has productDownloadURL ivar and a prepareForDownload method that returns an AFHTTPRequestOperation to the caller:

- (AFHTTPRequestOperation * )prepareForDownload:(NSString *)productIdentifier
{
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:_productDownloadURL]];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:productIdentifier];
    operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];

    return  operation;
}

My RootViewController make the request by using AFHTTPRequestOperation and sets downloadProgress/success/failure blocks as below:

AFHTTPRequestOperation *operation = [[InAppRageIAPHelper sharedHelper] prepareForDownload:productIdentifier];
[operation setDownloadProgressBlock:^(NSInteger bytesRead, NSInteger totalBytesRead, NSInteger totalBytesExpectedToRead) {
     float percentDone = ((float)((int)totalBytesRead) / (float)((int)totalBytesExpectedToRead));    
     [(UIProgressView *)_hud.customView setProgress:percentDone];
     _hud.labelText = [NSString stringWithFormat:@"%f",percentDone];
 }];

 [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
   _hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"success.png"]];
   [self performSelector:@selector(dismissHUD:) withObject:nil afterDelay:1.5];
 } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
   _hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"error.png"]];
 }];
 [operation start];

hud is a MBProgressHUD. You could also enhance the progress display using MBProgressHUDModeDeterminate mode.

网白 2025-01-13 22:47:58

从控制器发出请求,并在创建操作并将其排入变量时保留对操作的引用(您可以通过使用 HTTPOperationWithRequest:success:failure 创建中间操作对象来完成此操作,然后手动在enqueueHTTPOperation:

的主体中,设置进度视图的progress属性(需要划分) bytesReceived 通过 bytesExpectedToReceive 以便在 0.0 和 1.0 之间标准化。

Make the request from a controller, and keep a reference to the operation when you create and enqueue it in a variable (you can do this by creating an intermediary operation object with HTTPOperationWithRequest:success:failure, and manually doing enqueueHTTPOperation:.

In the body ofsetDownloadProgressBlock, set the progress property of the progress view (you need to divide bytesReceived by bytesExpectedToReceive in order to normalize between 0.0 and 1.0.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文