Search code examples
swifthttpswift3alamofire

Use Alamofire 4 to download an MP4 file, whilst attaching a custom header


I am trying to download MP4 data using the following code successfully:

private func downloadVideo(cat: String){
    DispatchQueue.global(qos: .background).async {
        let destination: DownloadRequest.DownloadFileDestination = { _, _ in
            var documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]

            let milPath = "VID.mp4"
            documentsURL.appendPathComponent(milPath)
            return (documentsURL, [.removePreviousFile])
        }

        Alamofire.download(self.url, to: destination).response { response in
            if response.destinationURL != nil {
                print(response.destinationURL!)
                let path = (response.destinationURL?.absoluteString)!
                self.uVid[cat] = path
                //self.playVideo(path);
                DispatchQueue.main.async {
                    let uim = self.uBlur[cat]
                    self.disappearView(im : uim!)
                }
            }
        }
    }
}

However, I am unable to attach a custom header that the server requires whilst providing me with the right mp4 data. Could somebody construct an example/advice on how I can call the Alamofire.download() func whilst passing in custom headers?


Solution

  • I managed to find a solution to this question.

    simple add the header array inbetween self.ur, ,to:destination