1

I would like to ask a question about download file issue. I want to show the download progress to user. Therefore, I implement the class and code as below in playground, please see [CODE 1].

When I execute the code as below [Scenario 1], we can get the result. I see that the content size value is return -1.

Afterwards, I execute the code as below [Scenario 2]. I see that the content size value is NOT -1.

What's wrong with here? It is because Advanced Rest Client of Chrome can get the "Content-Length" (Please see the [Refer 1]), the NSURLSession CANNOT.

I want to show the download progress of the html. Please help.

[Scenario 1]

var connectWebSite: webConnect

connectWebSite = webConnect()
connectWebSite.downloadData("http://www.nba.com")

[Scenario 1 - result] enter image description here

[Scenario 2]

var connectWebSite: webConnect

connectWebSite = webConnect()
connectWebSite.downloadData("http://releases.ubuntu.com/14.04.2/ubuntu-14.04.2-server-amd64.iso")

[Scenario 2 - result] enter image description here

[Reference 1] enter image description here

[CODE 1]

import UIKit
import SystemConfiguration
import XCPlayground
import Foundation

XCPSetExecutionShouldContinueIndefinitely()

import Foundation

class webConnect: NSObject, NSURLSessionDelegate, NSURLSessionTaskDelegate ,NSURLSessionDataDelegate, NSURLSessionDownloadDelegate
{
    var mainSession : NSURLSession = NSURLSession()

    override init()
    {
        super.init()
        var configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
        mainSession = NSURLSession(configuration: configuration,
            delegate: self,
            delegateQueue:NSOperationQueue.mainQueue())
    }

    /* delegate function during download */
    func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {

        var x: NSHTTPURLResponse

        x = downloadTask.response as NSHTTPURLResponse

        println("Content size - \(x.expectedContentLength)")
        println("download status - \(bytesWritten) \(totalBytesWritten) \(totalBytesExpectedToWrite) ")
    }

    /* delegate function fire when download completed */
    func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL)
    {
        var locateData: NSData
        var result: String

        locateData = NSData(contentsOfURL: location)!
        result = NSString(data: locateData, encoding: NSASCIIStringEncoding)!

        println("final result")
    }

    /* delegate function fire when error occur during download */
    func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) {
        if (error != nil)
        {
            println("error - \(error?.description)")
        }
    }

    func downloadData(URLString: String)
    {
        var request = NSMutableURLRequest(URL: NSURL(string: URLString)!)
        var result : String = ""


        var task = self.mainSession.downloadTaskWithRequest(request)
        task.resume()

    }
}
var connectWebSite: webConnect

connectWebSite = webConnect()
/* Scenario 1*/
connectWebSite.downloadData("http://www.nba.com")
/* Scenario 2*/
connectWebSite.downloadData("http://releases.ubuntu.com/14.04.2/ubuntu-14.04.2-server-amd64.iso")
Tommy Liu
  • 239
  • 4
  • 13

0 Answers0