Redracer68 Redracer68 - 1 month ago 10
Swift Question

Alamofire download in MacOS. Looks fine, progresses is working, but no file? Swift

I have a strange little issue.

I have Alamofire added through cocoapods and I have a method going to download a zip file (approx 50Mb).
While downloading everything looks perfect. I can see in Activity Monitor that 50Mb is downloaded for my app, I can see the progressbar zinging across. But I can never find the file.
Right now I have it set to use the current directory, but have tried others just in case. I have even searched the entire drive by data modified and never find anything.

I would love any help that can be offered here. Can't find anything really dealing with my situation on SO.

Here is my code.

func downloadAndInstall(){
log.info("Downloading and Installing.....")
displayToUser(content: "Downloading and Installing.....")

let urlString = updatePackageURL //(This is http://xxxx.com/xxxxpackage.zip)
let fileManager = FileManager.default
currentDir = fileManager.currentDirectoryPath
let fileURL: URL = URL(string: currentDir + "/package.zip")!
let destination: DownloadRequest.DownloadFileDestination = { _, _ in (fileURL, []) }
log.info("FILEURL: \(fileURL)")
var progressValues: [Double] = []
var response: DefaultDownloadResponse?

Alamofire.download(urlString, to: destination)
.downloadProgress { progress in
progressValues.append(progress.fractionCompleted)
log.info("Latest Progress Value: \(progress.fractionCompleted)")
self.progBar.doubleValue = progress.fractionCompleted
}
.response { resp in
response = resp
if progressValues.last != 1.0 {
//backout of the process, something went wrong
log.debug("Something went wrong downloading the file. Close and try again.")
self.displayToUser(content: "Something went wrong downloading the file. Close and try again.")
self.exitpoorly()
}
else{
log.info("Download Finished")
self.displayToUser(content: "Download Finished")
self.extractpackage()
}
}

var previousProgress: Double = progressValues.first ?? 0.0

for progress in progressValues {
previousProgress = progress
}

if let lastProgressValue = progressValues.last {
log.info("Current Download Value: \(lastProgressValue, 1.0)")
} else {
//Fail
}
}

Rob Rob
Answer

I'd suggest checking for any errors, e.g.:

Alamofire.download(urlString, to: destination)
    .downloadProgress { progress in
        ...
    }
    .response { response in
        guard response.error == nil else {
            //backout of the process, something went wrong
            log.debug("Something went wrong downloading the file.  Close and try again.")
            log.debug(response.error!.localizedDescription)
            ...
            self.exitpoorly()
        } else {
            log.info("Download Finished")
            ...
        }
}

Maybe the app is sandboxed, or perhaps you don't have permissions for that folder. It's hard to say without seeing the error.