Getting started with Download Manager in Swift

Hardik Modha
Mindful Engineering
3 min readJun 29, 2021

In this article let me show you how easy is to download the audio or any other media file from the URL and store it into the local database.

Photo by Markus Winkler on Unsplash

For this blog, I am downloading audio files from the URL, but you can download any media file using Download Manager with minor changes in code.

So Let’s start,

In this blog, I am going to explain three main aspects of a Download manager.

This article covers three main aspects:-

  • How to Download a File
  • How to Store a File
  • How to Read a File
  1. How to Download a File

First, we need to create a URLSession instance

URLSession

After getting the list we are going a step forward to create a URLSession object. The basic use of the URLSession object is to provide a start, pause and resume file.

URLSessionDownloadDelegate:-

Create an instance of URLSession in your controller file and implement URLSession delegate methods in that file.

didFinish delegate method will call once downloading is finish and will provide the location of the downloaded file.

We just copy that item from a downloaded location in our document directory.

didWriteData delegate method will call when download is going on and it will provide the total bytes you have written and the total bytes you expect to write.

This method is used to update tasks such as to show the progress bar regarding completed download.

Now we are going to start downloading files, for that URLSession provides a method downloadTask, that will starts to download files.

Start download Task: -

The below code will start downloading a file

URLSession also provides the facility to pause and resume download tasks.

Pause the task:-

URLSession will provide an easy way to pause ongoing tasks. For that URLSession has method name cancel by providing resuming data.

The below code shows the functionality of pausing the ongoing task

Resume the task

With pausing functionality, URLSession also provides an easy way to resume the task from where downloading has stopped.

This can be achieved by downloadTask method to resume tasks.

Here we are done with the start, pause and resume download file.

Store download file in File System.

Now moving forward after completing the download task, we will store that file in our file system.

After storing the file in a file system, we are ready to play, pause and stop the song.

AVAudioPlayer

iOS has an InBuilt Framework called AVAudioPlayer which is easy to use and integrate so let’s use it and see how that helps us.

With the help of AVAudioPlayer we can easily manage to play, pause, and stop audio.

To play, pause and stop audio files using AVAudioPlayer let’s get started and go ahead to integrate it.

First, create a reference of the audio player as shown below -

Now we going to create AVAudioPlayer and play audio

For that below code will create a player and play the audio file-

After that to pause ongoing audio, use the below snippet-

Lastly, to stop the audio file, the below code is used-

Finally, it’s done 😎

Thanks for reading!

Happy Coding!

Source Code:-

https://github.com/Mindinventory/DownloadManager

Reference Link:-

--

--