Я использую этот код чтобы скачать файл.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"1.mp3"];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:false];
NSString *stringURL = @"link";
NSURL *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePath atomically:YES];
и этот чтобы узнать что файл существует
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:false];
Я хочу узнать когда загрузка этого файла была завершена(не когда файл существует в директории) и создать проверку по типу:
if (загрузка файла была завершена) {
//действие
}
Как это сделать?
UPD
if (indexPath.row == 1) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"1.mp3"];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:false];
if (!fileExists) {
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:@"1"
message:@"1"
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* actionAdd = [UIAlertAction
actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
_spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
_spinner.frame = CGRectMake(0, 0, 24, 24);
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryView = _spinner;
[tableView cellForRowAtIndexPath:indexPath].accessoryView = _spinner;
[_spinner startAnimating];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
NSString *dataUrl = @"link";
NSURL *url = [NSURL URLWithString:dataUrl];
NSURLSessionDataTask *downloadTask = [[NSURLSession sharedSession]
dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (((NSHTTPURLResponse *)response).statusCode == 200) {
[data writeToFile:filePath atomically:YES];
}
}];
[downloadTask resume];
});
dispatch_async(dispatch_get_main_queue(), ^{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (fileExists) {
[_spinner2 stopAnimating];
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
[cell setTintColor:[UIColor blackColor]];
}
});
});
}];
UIAlertAction* actionCancel = [UIAlertAction
actionWithTitle:@"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:actionAdd];
[alert addAction :actionCancel];
alert.popoverPresentationController.sourceView = self.view;
alert.popoverPresentationController.sourceRect = CGRectMake(self.view.bounds.size.width / 3.4, self.view.bounds.size.height / 4.0, 1.0, 1.0);
[self presentViewController:alert animated:YES completion:nil];
}
if (fileExists) {
[self performSegueWithIdentifier: @"Segue" sender: self];
}
}
dataWithContentsOfURL
? Нужно использоватьNSURLSessionDataTask
?