ios swift download video from url to phone gallery

3257 views ios

I am using a URLSessionDownloadTask to download a Video from url to my phone gallery, how can i set the destination path for the downloaded Video file, i need it to appear in my Phone Video folder.

var downloadTask: URLSessionDownloadTask!
var backgroundSession: URLSession!

if let theUrl = url {

  downloadTask = backgroundSession.downloadTask(with: theUrl)

extension DownloadVideoViewController : URLSessionDownloadDelegate {

    func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {

        if self.downloadTask != nil {
            self.displyAlertWithAction(title: "Complete", message: "Download finished")

    func urlSession(_ session: URLSession,downloadTask: URLSessionDownloadTask,didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64)
        print("\nDownload started ....Total is \(Float(totalBytesExpectedToWrite)/(1024*1024)) Mb")
        print("\nWritten \(totalBytesWritten) bytes")
        progressView.setProgress(Float(totalBytesWritten)/Float(totalBytesExpectedToWrite), animated: true)

    func urlSession(_ session: URLSession,task: URLSessionTask,didCompleteWithError error: Error?)
        if (error != nil) {
            if downloadTask != nil {
                print("The task finished transferring data successfully")
                downloadTask = nil
                progressView.setProgress(0.0, animated: true)



answered question

Have an answer?


Please login first before posting an answer.