I am trying to use Alamofire4 for Swift3 . I need to download mp3 files and save them in the Documents directory. The current code looks like this:
func downloadAudioFromURL(url: String, completion: ((_ status: ResponseStatus, _ audioLocalURL: URL?) -> Void)?) { let fileManager = FileManager.default let directoryURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0] let audioFullURL = String.ensureFullURLPath(url) alamoManager.download(audioFullURL) .validate { request, response, temporaryURL, destinationURL in var pathComponent = response.suggestedFilename! if pathComponent == "m4a.mp4" {
However, I cannot access the files from localURL after saving. I also noticed that localURL will be exactly the same for the different files I am trying to download (maybe they are overwritten?). For example: file:///Users/testuser/Library/Developer/CoreSimulator/Devices/D4254AEA-76DD-4F01-80AF-F1AF3BE8A204/data/Containers/Data/Application/29755154-DD21-4D4C-B340-6628607DC053/Documents/file1.mp3
Any ideas what I'm doing wrong here? Any pointers would be really appreciated! thanks!
Edited my code to look like this:
func downloadAudioFromURL(url: String, completion: ((_ status: ResponseStatus, _ audioLocalURL: URL?) -> Void)?) { let destination: DownloadRequest.DownloadFileDestination = { _, _ in var documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] documentsURL.appendPathComponent("Audiofile.mp3") return (documentsURL, [.removePreviousFile]) } Alamofire.download(url, to: destination).response { response in if let localURL = response.destinationURL { completion?(.success, localURL) } else { completion?(.failure, nil) } } }
How can I check m4a.mp4 though?
source share