Search code examples
iosswiftnsurlsessionnsurlsessiondownloadtask

Download a file with NSURLSession in Swift


i have like 2 problems here , first i cant set NSURLSessionDownloadDelegate with a swift project, compiler says

Type 'ViewController' does not conform to protocol 'NSURLSessionDownloadDelegate'

Second problem is i cant find NSURLSession methods to download a simple file

here is the way i use to download the simple file

    var url:NSURL = NSURL.URLWithString(fileURL)
    var request:NSURLRequest = NSURLRequest(URL: url)
    var downloadTask:NSURLSessionDownloadTask = sessionManager.downloadTaskWithRequest(request)
    downloadTask.resume()

and these are the methods i want to make in swift

URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite

..

URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location

..

URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error

.. if there is a new way to download files with NSURLSession i would like to know , and whats new in NSURLSession in swift


Solution

  • I am at the moment on a project with a background Download Manager, and here are a few things, how I solved that:

    if you are using the NSURLSessionDownloadDelegate you need to implement the following methods:

    func URLSession(session: NSURLSession!, downloadTask: NSURLSessionDownloadTask!, didFinishDownloadingToURL location: NSURL!) 
    
    func URLSession(session: NSURLSession!, downloadTask: NSURLSessionDownloadTask!, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) 
    
    func URLSession(session: NSURLSession!, downloadTask: NSURLSessionDownloadTask!, didFinishDownloadingToURL location: NSURL!) 
    

    I have done this with this call:

    var session:NSURLSession!
    
    
        var sessionConfiguration:NSURLSessionConfiguration = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("com.company")
        sessionConfiguration.HTTPMaximumConnectionsPerHost = 5
    
        self.session = NSURLSession(configuration: sessionConfiguration, delegate: self, delegateQueue: nil)
    

    // on download

    var downloadTask:NSURLSessionDownloadTask = self.session.downloadTaskWithURL(NSURL.URLWithString("urlfromyourfile"))
    downloadTask.resume()
    

    // on error:

    func URLSession(session: NSURLSession!, task: NSURLSessionTask!, didCompleteWithError error: NSError!) {
    
        if(error != nil) {
    
            println("Download completed with error: \(error.localizedDescription)");
    
        } else {
    
            println("Download finished successfully");
    
        }
    
    }
    

    Here you find a good tutorial (i used lots of code from that tutorial and wrote it new with swift)

    http://www.appcoda.com/background-transfer-service-ios7/