本文介绍了如何使用AFNetworking AFHTTPRequestOperationManager显示ProgressBar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试显示进度条,因为我从网址下载了JSON。 JSON正在正确下载,但我不确定如何显示进度条。我尝试过使用 UIProgressView
但它没有显示在屏幕上。任何建议都将不胜感激。
I am trying to show the Progress Bar as I download a JSON from a url. The JSON is downloading correctly but I am not sure how to show the Progress Bar. I have tried using UIProgressView
but it does not display on the screen. Any suggestions would be appreciated.
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);
}];
推荐答案
在成功中设置download-progress-block块,这有点太晚了;)
You set the download-progress-block inside the success block, which is a bit too late ;)
试试这个:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
AFHTTPRequestOperation *operation = [manager GET:@"https:urlWithJson" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Complete");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
myProgressView.progress = (float)totalBytesRead / totalBytesExpectedToRead;
}];
这篇关于如何使用AFNetworking AFHTTPRequestOperationManager显示ProgressBar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!