Two types of download tasks

To download HLS streams you will need to create a URLSessionTask, specificly designed for this task. AVAssetDownloadURLSession offers two types:

In essence, the difference is between the two comes down to AVAssetDownloadTask only allowing you to download the HLS stream with a single media selections per type (such as audio tracks and subtitles), where AVAggregateAssetDownloadTask allows you to include multiple media selections.

Creating an AVAssetDownloadTask or AVAggregateAssetDownloadTask

The two types of download tasks are created as following:

// AVAssetDownloadTask
func makeAssetDownloadTask(asset: AVURLAsset,
                           assetTitle: String,
                           assetArtworkData: Data?,
                           options: [String : Any]?) -> AVAssetDownloadTask?

/// AVAggregateAssetDownloadTask
func aggregateAssetDownloadTask(with URLAsset: AVURLAsset,
                                mediaSelections: [AVMediaSelection],
                                assetTitle title: String,
                                assetArtworkData artworkData: Data?,
                                options: [String : Any]? = nil) -> AVAggregateAssetDownloadTask?

Both download tasks require at least an AVURLAsset and an human-readable title. When creating an AVAggregateAssetDownloadTask you will also need to at least pass one AVMediaSelection.

Optionally, an image can be passed as Data, which I highly recommend. Both the title and the assetArtworkData are there to help your users recognize the downloaded HLS streams. The Settings application shows downloaded videos in its storage section and uses the title and the assetArtworkData.

Adding options

Both types of download tasks allow you to add options as [String : Any]. Possible options are:

public class AVAssetDownloadTask {
	public let AVAssetDownloadTaskMinimumRequiredMediaBitrateKey: String

	// Starting in iOS 14
	public let AVAssetDownloadTaskMinimRequiredPresentationSizeKey: String
	public let AvAssetDownloadTaskPrefersHDRKey: String
}

Including media selections to AVAssetDownloadTask

HLS streams can contain multiple playback tracks, to support, for example, alternative audible tracks and subtitle tracks. When downloading an HLS stream using AVAssetDownloadTask, only the default playback tracks will be included. To include an alternative track, it supports an additional option:

public let AVAssetDownloadTaskMediaSelectionKey: String

The biggest downside of this is that you can only add one alternative media selection to an AVAssetDownloadTask. To download multiple playback tracks using AVAssetDownloadTask you will need to run multiple consecutive download tasks.

Including media selections to AVAggregateAssetDownloadTask

AVAggregateAssetDownloadTask does support downloading multiple media selections by passing [AVMediaSelection]. AVURLAsset offers two convenient selections which you can directly pass as a parameter when creating an AVAggregateAssetDownloadTask. These are:

// The media selection preferred by the stream
let preferredMediaSelections: AVMediaSelection = asset.preferredMediaSelection
// All available media selections
let allMediaSelections: [AVMediaSelection] = asset.allMediaSelections