Я пытаюсь рассчитать прогресс моего метода загрузки и показать прогресс MBProgressHUD
во время загрузки файла, но я не знаю, как рассчитать ход выполнения! Вот мой код:Получите прогресс MBProgressHUD от NSURL
- (IBAction)preview:(id)sender {
HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
[self.navigationController.view addSubview:HUD];
// Set determinate mode
HUD.mode = MBProgressHUDModeAnnularDeterminate;
HUD.delegate = self;
HUD.labelText = @"Loading";
// myProgressTask uses the HUD instance to update progress
[HUD showWhileExecuting:@selector(downloadDataFromMac) onTarget:self withObject:nil animated:YES];
}
- (void)downloadData {
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSURL *URL = [NSURL URLWithString:pathWithData];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
NSLog(@"File downloaded to: %@", filePath);
//Hide HUD
[HUD hide:YES];
self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:filePath];
[self.documentInteractionController setDelegate:self];
[self.documentInteractionController presentPreviewAnimated:YES];
}];
[downloadTask resume];
}
EDITED:
- (void)downloadDataFromMac {
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSURL *URL = [NSURL URLWithString:pathWithData];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSProgress *progress;
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:&progress destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
[progress addObserver:self
forKeyPath:@"fractionCompleted"
options:NSKeyValueObservingOptionNew
context:NULL];
NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
NSLog(@"File downloaded to: %@", filePath);
//Hide HUD
[HUD hide:YES];
self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:filePath];
[self.documentInteractionController setDelegate:self];
[self.documentInteractionController presentPreviewAnimated:YES];
}];
[downloadTask resume];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
if ([keyPath isEqualToString:@"fractionCompleted"]) {
NSProgress *progress = (NSProgress *)object;
NSLog(@"Progress… %f", progress.fractionCompleted);
//do something with your progress here, for eg :
//but dont forget to first make HUD a class property so you can update it
[HUD setProgress:progress.fractionCompleted];
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
Я загрузил фреймворк, но и слежу за установкой, но все равно получаю эту ошибку 'RSNetworkKit.h не найден' !!!!!!!!! –
вы пробовали с импортом? –
iOSEnthusiatic
Да! Кажется, вы разработчик, пожалуйста, пришлите мне пример проекта? Почему вы так усложнили установку?! –