ios – NSURLSession代理未被调用

前端之家收集整理的这篇文章主要介绍了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.

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

原文链接:https://www.f2er.com/iOS/336691.html

猜你在找的iOS相关文章