gpt4 book ai didi

ios - 如何在 downloadTaskWithURL 完成之前获取 NSURLResponse?

转载 作者:可可西里 更新时间:2023-11-01 00:53:06 25 4
gpt4 key购买 nike

这是我的下载代码:

let url = NSURL(string:"http://www.zastavki.com/pictures/originals/2013/Photoshop_Image_of_the_horse_053857_.jpg")!

let documentsDirectoryURL = NSFileManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first as! NSURL

NSURLSession.sharedSession().downloadTaskWithURL(url, completionHandler: {
(location, response, error) -> Void in

if let error = error {
println(error.description)
}
else {
println("Finished downloading \"\(response.suggestedFilename)\".")
println(location.path!)
println("Started saving \"\(response.suggestedFilename)\".")
if NSFileManager().moveItemAtURL(location, toURL: documentsDirectoryURL.URLByAppendingPathComponent(response.suggestedFilename!), error: nil) {
println("File saved")
} else {
println("The File \(response.suggestedFilename!) was not saved.")
}

}
}).resume()

就目前而言,响应只能在完成处理程序中访问。

我的问题是如何在下载完成之前访问响应?

我需要 NSURLResponse 来知道:

  • 预期内容长度
  • 建议的文件名
  • MIME类型

最佳答案

不要使用共享 session

保留一个 session 属性,使用这个函数来初始化。

 init(configuration configuration: NSURLSessionConfiguration?,
delegate delegate: NSURLSessionDelegate?,
delegateQueue queue: NSOperationQueue?) -> NSURLSession

然后使用dataTask下载图片

在这个委托(delegate)方法中你可以获得Response

然后将dataTask改为downlaodTask

optional func URLSession(_ session: NSURLSession,
dataTask dataTask: NSURLSessionDataTask,
didReceiveResponse response: NSURLResponse,
completionHandler completionHandler: (NSURLSessionResponseDisposition) -> Void)

示例代码:

 import UIKit
class ViewController: UIViewController,NSURLSessionDelegate,NSURLSessionDataDelegate,NSURLSessionDownloadDelegate{
var session:NSURLSession?
var dataTask:NSURLSessionDataTask?
let url = NSURL(string:"http://www.zastavki.com/pictures/originals/2013/Photoshop_Image_of_the_horse_053857_.jpg")!
var infoDic = NSMutableDictionary()
override func viewDidLoad() {
super.viewDidLoad()
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
let manqueue = NSOperationQueue.mainQueue()
session = NSURLSession(configuration: configuration, delegate:self, delegateQueue: manqueue)
dataTask = session?.dataTaskWithRequest(NSURLRequest(URL: url))
dataTask?.resume()

// Do any additional setup after loading the view, typically from a nib.
}
func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveResponse response: NSURLResponse, completionHandler: (NSURLSessionResponseDisposition) -> Void) {
NSLog("%@",response.description)
completionHandler(NSURLSessionResponseDisposition.BecomeDownload)
}
func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didBecomeDownloadTask downloadTask: NSURLSessionDownloadTask) {
downloadTask.resume()
}
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL) {
NSLog("%@",location);
//Get response
NSLog("%@", downloadTask.response!.description)

}
}

关于ios - 如何在 downloadTaskWithURL 完成之前获取 NSURLResponse?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30047706/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com