Download with AVAssetDownloadTask gets me an error on URLSession - iOS 10.x

1.3k Views Asked by At

I'm following Apple Media Playback Programming Guide example.

The following code works on iOS 11, but crash on iOS 10.

I setup the download as following:

func setupAssetDownload(for item: DownloadItem) {
    let url = URL(string: item.urlVideo!)!
    let asset = AVURLAsset(url: url)

    // Create new AVAssetDownloadTask for the desired asset
    guard let task = downloadSession.makeAssetDownloadTask(asset: asset,
                                                           assetTitle: item.title!,
                                                           assetArtworkData: nil,
                                                           options: [AVAssetDownloadTaskMinimumRequiredMediaBitrateKey: 265_000])
        else { return }

    // Start task and begin download
    task.resume()
}

And fails here:

func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
    /*
     This is the ideal place to begin downloading additional media selections
     once the asset itself has finished downloading.
     */
    if let error = error as NSError? {
        switch (error.domain, error.code) {
        case (NSURLErrorDomain, NSURLErrorCancelled):
            /*
             This task was canceled, you should perform cleanup using the
             URL saved from AVAssetDownloadDelegate.urlSession(_:assetDownloadTask:didFinishDownloadingTo:).
             */
            ...

        case (NSURLErrorDomain, NSURLErrorUnknown):
            fatalError("Downloading HLS streams is not supported in the simulator.")

        default:
            fatalError("An unexpected error occured \n\(error.code) - \(error.domain) - \(error.localizedDescription)")
        }
    } else {
        // downloaded
    }
}

I get the following error:

Error Domain=AVFoundationErrorDomain 
Code=-11800 \"The operation could not be completed\" 
UserInfo={
    NSLocalizedDescription=The operation could not be completed,
    NSLocalizedFailureReason=An unknown error occurred (-12780)
}

Could someone point me on how to solve this?

Note: I read something about using fileURLWithPath instead of string for composing the URL, but didn't work at all.

Not related with NSAppTransportSecurity.

0

There are 0 best solutions below