首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用MBProgressHud更新AFHTTPRequestOperation

如何用MBProgressHud更新AFHTTPRequestOperation
EN

Stack Overflow用户
提问于 2012-02-17 12:28:28
回答 2查看 3.8K关注 0票数 3

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

这些例子包括:

  • AFNetworking;
  • Inapp购买(从RayW);
  • MBProgressHud;

在我的视图控制器中,我推送了一个UIbutton,它触发了Inapp购买Singleton示例,并开始使用AFHTTPRequestOperation从我的服务器下载一个文件。沟通的这一部分起作用了。但是我想要实现的是在下载时更新我的hud。因为文件是>10 As。

因此,问题是如何根据下载的进度更新hud?我试着把它拉下来。

  • 按下视图控制器中的按钮,就会显示hud;
代码语言:javascript
复制
- --> request will sent to the Singleton InApp helper class which handles the networking part;
代码语言:javascript
复制
    - --> After that the AFHTTPRequestOperation will be called inside the singleton class for the download of file; 
代码语言:javascript
复制
        - ---> During this download i use the setDownloadProgressBlock method for the progress. 

但是,如何将进度信息返回到视图控制器中的hud?

谢谢。

EN

回答 2

Stack Overflow用户

发布于 2012-03-12 17:40:08

这就是我对类似的问题所做的,遵循@mattt的建议。我的Singleton InApp helperproductDownloadURL ivar和一个向调用者返回AFHTTPRequestOperationprepareForDownload方法:

代码语言:javascript
复制
- (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块设置如下:

代码语言:javascript
复制
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模式增强进度显示。

票数 3
EN

Stack Overflow用户

发布于 2012-02-20 20:47:11

从控制器发出请求,并在变量中创建操作并将其放入队列时保持对操作的引用(您可以通过使用HTTPOperationWithRequest:success:failure创建中间操作对象并手动执行enqueueHTTPOperation:来做到这一点。

setDownloadProgressBlock的主体中,设置进度视图的progress属性(您需要将bytesReceived除以bytesExpectedToReceive,以便在0.0和1.0之间正常化。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9328204

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档