我正在开发一个应用程序,用户可以下载书籍,当用户开始下载他仍然在屏幕(模态屏幕),直到下载完成,但如果它是一本大书?这将是一个问题所以我希望用户关闭屏幕,但监视SlidingMenu或ViewController的下载进度,如果他们是多个文件也可以监视他们的进度,我使用此代码在同一屏幕下载文件。
NSString *pathURL=item.path;
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:pathURL]];
operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:item.fileName];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];
[operation setDownloadProgressBlock:^(NSUInteger bytesRead, NSInteger totalBytesRead, NSInteger totalBytesExpectedToRead) {
mbProcess.progress=(float)totalBytesRead / totalBytesExpectedToRead;
_loadinglabel.text=[[NSString alloc]initWithFormat:@"Downloading %d %",(int)(100*((float)totalBytesRead/totalBytesExpectedToRead))];
_progressDialogdet.progress=(float)totalBytesRead/totalBytesExpectedToRead;
}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Successfully downloaded file to %@", path);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
// [mbProcess removeFromSuperview];
}];
[operation start];