ios - 使用 Alamofire 4.0 (Swift 3) 下载文件

标签 ios swift swift3 alamofire alamofireimage

在旧版本的 Alamofire 中。这是我下载文件的方式

    let destinationPath = Alamofire.Request.suggestedDownloadDestination( directory: .documentDirectory, domain: .userDomainMask);

    Alamofire.download(.GET, urlString, destination: destinationPath)
        .progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
//                print(totalBytesRead)
        }
        .response { request, response, _, error in

            let downloadedFilePath = destinationPath(URL(string: "")!, response!);

            NSUserDefaultsHelper.saveURL(downloadedFilePath, key: urlString);

            completion(downloadedFilePath, true);
    }

但是现在在新版本中,我的代码完全无法使用,Alamofire库中也没有类似的功能。

有什么想法吗?

最佳答案

我曾经使用过这个语句:

let destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory)

Alamofire.download(
    url,
    method: .get,
    parameters: parameters,
    encoding: JSONEncoding.default,
    headers: nil,
    to: destination).downloadProgress(closure: { (progress) in
        //progress closure
    }).response(completionHandler: { (DefaultDownloadResponse) in
        //here you able to access the DefaultDownloadResponse
        //result closure
    })

有关更多详细信息,请阅读 Alamofire docs about Migration to 4.0 :

关于ios - 使用 Alamofire 4.0 (Swift 3) 下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39912905/

相关文章:

swift - 带有 JSQMessageViewController 的选项卡栏

ios - 如何注册推送通知? iOS 10

ios - 无法以特定大小以模态方式呈现 View Controller

swift - 使用 Firebase Firestore 更新 TableViewCell 中的按钮 UI

ios - swift 3 错误 : "Binary operator ' /' cannot be applied to two ' int' operands"

ios - 使用 AVFoundation 在时域中记录位置

ios - NSNetService 发布,但从未调用过 didAcceptConnectionWithInputStream...

android - Ok.ru 与 ios 的集成 - 重定向 URI 错误

ios - NSPredicate 过滤器对多与子对象属性

ios - 如何以编程方式将 UIBarButtonItem 添加到我的导航 Controller ?