我有TableViewController
和ViewController
。我想在下載音頻後進入ViewController
。但是我現在正在進行音頻下載。我使用fileExists
它檢查是否下載音頻。但它不起作用。如何在ViewController
下載音頻後進入?如何轉到ViewController?
這是我下載的代碼去
if (!fileExists) {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Download audio?" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* actionAdd = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
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 = @"https://drive.google.com/uc?export=download&id=0B6zMam2kAK39R2Z5RlVWZkN3Vzg";
NSURL *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePath atomically:YES];
});
if (fileExists) {
[self performSegueWithIdentifier: @"detailSegue" sender: self];
}
}];
UIAlertAction* actionCancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
[alert addAction:actionAdd];
[alert addAction :actionCancel];
[self presentViewController:alert animated:YES completion:nil];
}
if (fileExists) {
[self performSegueWithIdentifier: @"detailSegue" sender: self];
}
}
如果添加(FILEEXISTS){ [自performSegueWithIdentifier:@ 「detailSegue」 發件人:自我]。 }在你的dispatch_async代碼塊 –
而不是'dataWithContentsOfURL'你應該使用NSURLSession'downloadTaskWithURL'或'dataTaskWithURL'這將給你委託方法或完成塊,你可以做下載完成後你必須做的編碼 – HardikDG