## Manage Offline Content For Playback

One of the challenges of enabling offline playback is downloading and managing assets. The Bitmovin SDK provides the ability to download content, as well as to pause, resume and cancel downloads. It also enables downloaded content to be deleted. These operations are provided via the `OfflineContentManager` API, which is responsible for managing the state of the assets. Application lifecycle events such as the application going into background, being terminated by the user, or crashing are handled by the `OfflineManager`.

## Initialize the OfflineManager

You first need to initialize the singleton class `OfflineManager` in your `AppDelegate` class. To do so, you add the following code snippet in the `application(_:didFinishLaunchingWithOptions:)` method:



## App Lifecycle Support

In order to appropriately handle application lifecycle events, such as the application going into background, add the following code into `application(_:handleEventsForBackgroundURLSession:completionHandler:)` in your `AppDelegate`:



## Download protected or unprotected content

Before you can download the content, you first need to create an instance of a `SourceConfig `that represents the asset to be downloaded.



In case the source is protected by Digital Rights Management (DRM), you just need to provide its DRM configuration like you would do for online playback:



You will also need a reference to the `OfflineManager` using the `sharedInstance()` method, which you can then use to get access to an `OfflineContentManager` for your `SourceConfig`. The `OfflineContentManager` instance manages offline content and offline DRM related tasks for this single `SourceConfig`:



Now that you have both the offline content manager and source config for the asset, you can already kick off the download operation, by calling the `download()` method.



## OfflineState

You can check the state of the download operation with the `offlineState` property, which is very useful for checking on the state of an asset before performing further actions. For example, checking the asset's offline state before updating the UI or allowing the user to cancel a download:



## Events

Additionally, the SDK allows you to add event listeners for tracking all the offline states mentioned above.



And then you can track the events for further action:



You can find more information about each of the above events in our documentation:

  • `onOfflineError`

  • `onContentDownloadFinished`

  • `onContentDownloadProgressChanged`

  • `onContentDownloadSuspended`

  • `onContentDownloadResumed`

  • `onContentDownloadCanceled`

## Cancel an ongoing download

The `OfflineContentManager` offers the `cancelDownload()` method that cancels a running or suspended download operation. When a download was canceled successfully, also all partially downloaded data is deleted automatically.



## Suspend an ongoing download

The `suspendDownload()` method can be used to suspend (pause) an ongoing download operation.



## Resume a suspended download

If the download of a given source config has `.suspended state`, it can be resumed at any time with the `resumeDownload()` method:



## Delete all downloaded data

You can force to delete all cached data for a given source config with the `deleteOfflineData()` method:



## Overview of states and allowed method calls

The following table outlines all allowed actions in all possible offline states a source config can have. It also shows which listener methods are called on the `OfflineContentManagerListener`:

Current StateMethod call triggering transitionOfflineContentManagerListener EventFollowing State
NotDownloadeddownload
Downloading
DownloadingcancelDownload
Canceling
DownloadingsuspendDownloadonContentDownloadSuspendedSuspended
Downloading
onContentDownloadFinishedDownloaded
Downloading
onContentDownloadProgressChangedDownloading
DownloadeddeleteOfflineData
NotDownloaded
SuspendedresumeDownloadonContentDownloadResumedDownloading
SuspendedcancelDownload
Canceling
Canceling
onContentDownloadCanceledNotDownloaded
DownloadedrenewOfflineLicenseonOfflineContentLicenseRenewedDownloaded

## Play online/offline content seamlessly

Now that you know how to manage assets for offline playback, you can focus on the what is needed to play back the asset. Assuming the offline state of of the offline content manager for the source config is `.downloaded`, `.downloading `or `.suspended`, before passing the source config to the player, you should check whether the device has access to the internet or not. If the device does not have internet access, the method `offlineContentManager.createOfflineSourceConfig(restrictedToAssetCache:)` can be used to create an offline source config from the initial downloaded source config:



This method gives you the option to `restrictToAssetCache`, which will restrict the playback to the audio and subtitle tracks which are cached on disk. As a result, tracks which are not cached, do not show up as selectable in the player UI. If the device has access to the internet, you should probably set `false` to this parameter as it would allow the user to select tracks that are not cached yet. In the case that the offline state for the offline content manager for the source config is `.notDownloaded` or `.canceling`, and the device does not have access to the internet, then the playback will not be possible and you should notify the user. Once these checks are done and playback is possible, you can load the source config into the player:



## Sample App

[Here](🔗) you will find a set of iOS sample applications. There is also a [dedicated app](🔗) that demonstrates the offline playback capabilities.