为了学习Ios,我下载了一些例子。
这些例子包括:
在我的视图控制器中,我推送了一个UIbutton,它触发了Inapp购买Singleton示例,并开始使用AFHTTPRequestOperation从我的服务器下载一个文件。沟通的这一部分起作用了。但是我想要实现的是在下载时更新我的hud。因为文件是>10 As。
因此,问题是如何根据下载的进度更新hud?我试着把它拉下来。
- --> 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.
但是,如何将进度信息返回到视图控制器中的hud?
谢谢。
发布于 2012-03-12 17:40:08
这就是我对类似的问题所做的,遵循@mattt的建议。我的Singleton InApp helper有productDownloadURL ivar和一个向调用者返回AFHTTPRequestOperation的prepareForDownload方法:
- (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模式增强进度显示。
发布于 2012-02-20 20:47:11
从控制器发出请求,并在变量中创建操作并将其放入队列时保持对操作的引用(您可以通过使用HTTPOperationWithRequest:success:failure创建中间操作对象并手动执行enqueueHTTPOperation:来做到这一点。
在setDownloadProgressBlock的主体中,设置进度视图的progress属性(您需要将bytesReceived除以bytesExpectedToReceive,以便在0.0和1.0之间正常化。
https://stackoverflow.com/questions/9328204
复制相似问题