Alamofire 4.0 下载文件

github上关于下载文件文档如下:

// Alamofire 3
let destination = Request.suggestedDownloadDestination()

Alamofire.download(.GET, urlString, destination: destination).response { request, response, data, error in
    // What is fileURL...not easy to get
    print(request)
    print(response)
    print(data)
    print(error)
}

// Alamofire 4
let destination = Request.suggestedDownloadDestination()

Alamofire.download(urlString, to: destination).response { response in // method defaults to `.get`
    print(response.request)
    print(response.response)
    print(response.temporaryURL)
    print(response.destinationURL)
    print(response.error)
    }

使用时发现urlString不是String类型,也不是URL类型,而是URLRequest类型,定义如下:

  let urlString = URLRequest(url: URL(string:downloadfileURL)!)

第二个参数destination在Request类下并不存在,通过源代码看出其在DownRequest类下,DownRequest继承Request类。
let filename : String = URL(string:downloadfileURL)!.lastPathComponent+".bin"
let fileUrl = directoryURL.appendingPathComponent(filename)


 let destination: DownloadRequest.DownloadFileDestination = { _, _ in
 return (fileUrl, [.createIntermediateDirectories, .removePreviousFile])
}

下载方法如下:


  Alamofire.download(urlString, to: destination).response {    response in // method defaults to `.get`
  
  }
  

终于搞定下载了,吐槽一下,用swift太坑了,swift3一升级几乎所有代码都要改一遍,心累。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容