4

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

zaph
  • 111,848
  • 21
  • 189
  • 228
Amr Mohamed
  • 2,290
  • 4
  • 20
  • 39

1 Answers1

8

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/

Yogesh
  • 1,565
  • 1
  • 19
  • 46
derdida
  • 14,784
  • 16
  • 90
  • 139
  • why xcode is saying `Type 'ViewController' does not conform to protocol 'NSURLSessionDownloadDelegate'` and if i write those methods it works with no error , is it a bug, and look to this question please you will know more about my problem [Myquestion](http://stackoverflow.com/questions/25574391/what-is-the-delegate-of-these-nsurlsession-methods-in-swift) – Amr Mohamed Aug 29 '14 at 19:12
  • When did XCode tell you this Error? – derdida Aug 29 '14 at 19:33
  • i used to add the delegates first in obj-c so that xcode will auto complete the methods of the delegate , but here in xcode 6 when i set this `NSURLSessionDownloadDelegate` the compiler tells me this error because i didn't write the delegates methods it's inversed i think ? – Amr Mohamed Aug 29 '14 at 19:44
  • You are using Swift and Obj-C in the same Project? – derdida Aug 29 '14 at 19:46
  • no i am updating my obj-c project to swift , it's a pure swift project – Amr Mohamed Aug 29 '14 at 19:52
  • So is your problem still present or not? Please check the tutorial i had posted, the delegate methods are the same in Swift and ObjC – derdida Aug 29 '14 at 20:00
  • no my problem is solved after i wrote those methods from you and thanks for that but xcode still not auto completing before i write those methods bla bla bla but at least i got the situation here , i already saw this post before with obj-C , thanks alot :) – Amr Mohamed Aug 29 '14 at 20:08
  • There is no suggestion to implement delegate methods on adopting the protocols as in the old XCode. It just shows `does not conform to protocol` error. You have to write/implement the methods on your own which you can find from the documentation if you don't remember. – iphondroid Apr 20 '15 at 12:49
  • https://dl.dropboxusercontent.com/u/2857188/BGTransferDemo.zip here is the link for the above URL's completed tutorial project. – Vin Jun 13 '17 at 06:30