【发布时间】:2015-06-28 08:28:45
【问题描述】:
我试图在从 url 下载 JSON 时显示进度条。 JSON 正在正确下载,但我不确定如何显示进度条。我试过使用UIProgressView,但它没有显示在屏幕上。任何建议将不胜感激。
CGFloat width = [UIScreen mainScreen].bounds.size.width;
CGFloat height = [UIScreen mainScreen].bounds.size.height;
CGRect rect = CGRectMake(width/2, height/2, width/5, height/5);
UIProgressView *myProgressView = [[UIProgressView alloc]initWithFrame:rect];
myProgressView.progressViewStyle = UIProgressViewStyleBar;
[self.view addSubview:myProgressView];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:@"https:urlWithJson" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead)
{
myProgressView.progress = (float)totalBytesRead / totalBytesExpectedToRead;
}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"operation Completed");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"ERROR");
}];
[operation start];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"timeout Error: %@", error);
}];
【问题讨论】:
-
totalBytesExpectedToRead 有值吗? HTTP 标头必须包含文档的大小才能获得预期的总字节数。
-
我尝试在 setDownloadProgressBlock 中放入一个 NSLog 语句,但它没有打印任何内容。有没有更好的方法可以实现我想要的?
标签: ios objective-c afnetworking-2 uiprogressview