ios – NSURLSession代理未被调用

在下面的代码中,文件下载就行了.然而,没有一个代理方法似乎被调用,因为我没有收到任何输出. progressView也没有更新.任何想法为什么?
import Foundation
import UIKit

class Podcast: PFQueryTableViewController,UINavigationControllerDelegate,MWFeedParserDelegate,UITableViewDataSource,NSURLSessionDelegate,NSURLSessionDownloadDelegate {

    func downloadEpisodeWithFeedItem(episodeURL: NSURL) {

    var request: NSURLRequest = NSURLRequest(URL: episodeURL)
    let config = NSURLSessionConfiguration.defaultSessionConfiguration()
    let session = NSURLSession(configuration: config,delegate: self,delegateQueue: nil)

    var downloadTask = session.downloadTaskWithURL(episodeURL,completionHandler: { (url,response,error) -> Void in
        println("task completed")
        if (error != nil) {
            println(error.localizedDescription)
        } else {
            println("no error")
            println(response)
        }
    })
    downloadTask.resume()

}

func URLSession(session: NSURLSession,downloadTask: NSURLSessionDownloadTask,didResumeAtOffset fileOffset: Int64,expectedTotalBytes: Int64) {
    println("didResumeAtOffset")
}

    func URLSession(session: NSURLSession,didWriteData bytesWritten: Int64,totalBytesWritten: Int64,totalBytesExpectedToWrite: Int64) {
          var downloadProgress = Double(totalBytesWritten) / Double(totalBytesExpectedToWrite)
    println(Float(downloadProgress))
    println("sup")

    epCell.progressView.progress = Float(downloadProgress)
}

     func URLSession(session: NSURLSession,didFinishDownloadingToURL location: NSURL) {
    println(location)

}
}

解决方法

从我的测试中,您必须选择是否要使用委托或完成处理程序 – 如果同时指定,只有完成处理程序被调用.这段代码给我运行进度更新和didFinishDownloadingToURL事件:
func downloadEpisodeWithFeedItem(episodeURL: NSURL) {
    let request: NSURLRequest = NSURLRequest(URL: episodeURL)
    let config = NSURLSessionConfiguration.defaultSessionConfiguration()
    let session = NSURLSession(configuration: config,delegateQueue: NSOperationQueue.mainQueue())

    let downloadTask = session.downloadTaskWithURL(episodeURL)
    downloadTask.resume()
}

func URLSession(session: NSURLSession,expectedTotalBytes: Int64) {
    println("didResumeAtOffset: \(fileOffset)")
}

func URLSession(session: NSURLSession,totalBytesExpectedToWrite: Int64) {
    var downloadProgress = Double(totalBytesWritten) / Double(totalBytesExpectedToWrite)
    println("downloadProgress: \(downloadProgress)")
}

func URLSession(session: NSURLSession,didFinishDownloadingToURL location: NSURL) {
    println("didFinishDownloadingToURL: \(location)")
    println(downloadTask)
}

NSURLSession documentation,这里是相关部分:

Like most networking APIs,the NSURLSession API is highly asynchronous. It returns data in one of two ways,depending on the methods you call:

  • To a completion handler block that returns data to your app when a transfer finishes successfully or with an error.
  • By calling methods on your custom delegate as the data is received.
  • By calling methods on your custom delegate when download to a file is complete.

因此,通过设计,它将数据返回到完成处理程序块或代理.但是在这里证明,不是两者.

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...