我在我的app目录中使用此代码下载文件。但我有问题。
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"file.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];
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
spinner.frame = CGRectMake(0, 0, 24, 24);
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryView = spinner;
[spinner startAnimating];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString *stringURL = @"link";
NSURL *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePath atomically:YES];
if (!fileExists) {
[self performSegueWithIdentifier: @"Segue" sender: self];
}
});
}];
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];
}
----------------------------------------------- -----------------------------------------
当文件被下载到目录UIActivityIndicatorView工作大约几分钟。怎么解决?
答案 0 :(得分:0)
很难诊断问题,如果这有助于您解决问题,请尝试使用此代码
我只编辑了你的dispatch_async ..代码
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString *stringURL = @"link";
NSURL *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePath atomically:YES];
dispatch_async(dispatch_get_main_queue(), ^{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryView = nil;
fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:false];
if (fileExists) {
[self performSegueWithIdentifier: @"Segue" sender: self];
}
});
});