iOS--三方技术

Alamofire 4.0 下载文件

2016-09-22  本文已影响3280人  Sultan

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一升级几乎所有代码都要改一遍,心累。

上一篇下一篇

猜你喜欢

热点阅读