使用 UIActivityIndi​​catorView 下载文件

file downloading with UIActivityIndicatorView

我使用此代码在我的应用程序目录中下载文件。但是我有问题。

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];
                     }

———————————————————————————————————————————— ——————————————————————————————————————————

当文件下载到 UIActivityIndi​​catorView 目录中时,工作大约几分钟。如何解决?

很难诊断问题,如果可以帮助您解决问题,请尝试此代码

我只编辑了你的 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];
            }
        });

    });