iosvideo-streaminghttp-live-streamingavassetdownloadtaskavassetdownloadstoragemanager

NSURLSessions and HLS video cacheing - partial downloads can't be deleted


I am attempting to cache an HLS video stream (not live) while a user watches it. To do this we set up an AvAssetDownloadTask similar to how apple recommends here:

https://developer.apple.com/library/content/documentation/AudioVideo/Conceptual/MediaPlaybackGuide/Contents/Resources/en.lproj/HTTPLiveStreaming/HTTPLiveStreaming.html

This method works well as we can pass the url asset from the download task to both download and stream simultaneously.

We want this download temporarily cached to the video downloads section of the app, this almost works natively with the NSUrlSessionTask delegate except for when we want to delete partial data.

If the download completes, we receive the final download location and can add an expiration time or manually delete the file later. However if a download only partially completes I cannot find a way to delete the partially downloaded video.

I was hoping cancelling and invalidating the task would clean up the partial download but this does not seem to be the case, further more apple states in the link above: In the case where a download is canceled, and there is no intention of resuming it, your app is responsible for deleting the portion of the asset already downloaded to a user’s device.

However there is not any recommendations of how to achieve this. It seems that deleting this partial download should be easily accessible but have yet to find a way to achieve this.

Manually iterating the video download directory does not seem to display these partial downloads either; yet they are visible in the 'downloaded videos' section of our app's settings in the main iOS settings.

Wondering if anyone else has solved this issue or has recommendations on how to accomplish.


Solution

  • Found the answer, instead of suspending a download task, it needs to be cancelled once we wish to complete the partial cache download, this will then fire

    - (void)URLSession:(NSURLSession *)session assetDownloadTask:(AVAssetDownloadTask *)assetDownloadTask didFinishDownloadingToURL:(NSURL *)location {

    In which the final url is provided.