gpt4 book ai didi

singleton - 如何使用 AFHTTPRequestOperation 更新 MBProgressHud

转载 作者:行者123 更新时间:2023-12-02 23:04:12 24 4
gpt4 key购买 nike

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

示例包括:

  • AF网络;
  • 应用内购买(来自 RayW);
  • MBProgressHud;

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

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

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

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

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

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

但是我如何将进度信息发送回 View Controller 中的HUD?

谢谢。

最佳答案

这就是我根据 @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 block 如下:

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];

hudMBProgressHUD 。您还可以使用 MBProgressHUDModeDeterminate 模式增强进度显示。

关于singleton - 如何使用 AFHTTPRequestOperation 更新 MBProgressHud,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9328204/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com