0

I have downloadTask, I'm a trying to download a video using downloadTask but it doesn't seem to work; whenever I download small files likes pictures or small videos it works perfectly but the videos I am now trying to download are really long, I even type the URL Explicitly but it doesn't work. Adding some breakpoints I realized it never gets to the closure, goes directly to the .resume(), but this happens specifically with these long videos. Please help, any ideas? Here's my code

@objc func downloadVideo() {
    guard let urlString = lessons[currentLesson].video_url, let videoURL = URL(string: urlString) else { return }
    URLSession.shared.downloadTask(with: videoURL) { fileURL, response, error in
        if let error = error{
            print("error \(error)")
        } else {
            print("Our local URL = \(fileURL)")
        }
    }.resume()
}

And here's the code the code that's calling this function:

private func configureNavigationBar() {
    navigationItem.rightBarButtonItems = [
        UIBarButtonItem(title: Constants.Strings.download, style: .done, target: self, action: #selector(downloadVideo)),
        UIBarButtonItem(image: UIImage(systemName: "icloud.and.arrow.down"), style: .done, target: self,
                        action: #selector(downloadVideo))
    ]
}
  • 1
    Because it take really long time, so the closure isn't called end yet? Check maybe the progress https://stackoverflow.com/questions/30543806/get-progress-from-datataskwithurl-in-swift ? – Larme Apr 04 '22 at 15:36
  • 1
    Easier to get the task returned by the download task, get its progress object and assign it to aUIProgressView observedProgress. Check this post https://stackoverflow.com/a/67309732/2303865 – Leo Dabus Apr 04 '22 at 15:57
  • Hmm I just tried both answers but I don't think the video is being stored locally ): and whenever I change it to URLDownloadTask it doesn't do anything – Fernando Ivan Perez Ruiz Apr 05 '22 at 01:18

0 Answers0