swift - 在 MacOS 中下载 Alamofire。看起来不错,进度正常,但没有文件?

标签 swift alamofire

我通过 cocoapods 添加了 Alamofire,我有一种方法可以下载一个 zip 文件(大约 50Mb)。

虽然下载一切看起来很完美。我可以在 Activity Monitor 中看到为我的应用程序下载了 50Mb,我可以看到进度条在快速移动。但我永远找不到该文件。

现在我将其设置为使用当前目录,但为了以防万一,我尝试了其他目录。我什至通过修改的数据搜索了整个驱动器,但从未找到任何东西。

这是我的代码。

    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
        }
    }

最佳答案

我建议检查是否有任何错误,例如:

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()
            return
        }

        log.info("Download Finished")
        ...
}

也许该应用程序已被沙盒化,或者您可能没有该文件夹的权限。没有看到错误很难说。

关于swift - 在 MacOS 中下载 Alamofire。看起来不错,进度正常,但没有文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40113961/

相关文章:

swift - 错误: Expression pattern of type 'CountableClosedRange<Int>' cannot match values of type 'UITextField!'

Swift 3、RxAlamofire 和映射到自定义对象

ios - 在WKWebview中突出显示文本时发生JavaScript异常

ios - 同步执行GET请求

ios - 当手机上没有安装 Facebook 应用程序时,如何在 iOS 应用程序中将照片分享到 Facebook?

ios - 如何在 Swift 4 中将条件实现到 UITableViewDataSource

json - 在 Swift 中使用来自 Rest API 的数组填充 UITableview

ios - JSONSerialization 在应用程序中返回 false,在 postman 中返回 true

php - Alamofire打印“由于错误,无法序列化JSON:”

ios - 如何向 uiviewcontroller 添加圆角半径