I'm creating a Internet Speed testing app and can't seem to get it right to upload the zip file to the ftp server. It is an open server. I want to be able to upload the file and show the progress.
The func URLSession(session: NSURLSession, task: NSURLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64)
isn't working.
import UIKit
import Foundation
class Upload: UIViewController, NSURLSessionTaskDelegate, NSURLSessionDataDelegate, NSURLSessionDelegate {
let url: NSURL = NSURL(string: "ftp://speedtest.tele2.net/upload")!
let path = NSBundle.mainBundle().pathForResource("2MB", ofType: "zip")
func Test() {
let data: NSData = NSData(contentsOfFile: path!)!
let request: NSMutableURLRequest = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringCacheData
request.timeoutInterval = 10.0
request.HTTPBody = data
let session: NSURLSession = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration(), delegate: self, delegateQueue: NSOperationQueue.mainQueue())
let task: NSURLSessionUploadTask = session.uploadTaskWithRequest(request, fromData: data)
task.resume()
}
func URLSession(session: NSURLSession, task: NSURLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) {
print("\(totalBytesSent)/\(totalBytesExpectedToSend)")
}
func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) {
print(error)
}
func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveResponse response: NSURLResponse, completionHandler: (NSURLSessionResponseDisposition) -> Void) {
completionHandler(NSURLSessionResponseDisposition.Allow)
}
func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveData data: NSData) {
//
}
}
Have you read the documentation properly. If you look at the documentation for this method,
Here, https://developer.apple.com/library/prerelease/ios/documentation/Foundation/Reference/NSURLSessionDataDelegate_protocol/index.html#//apple_ref/occ/intfm/NSURLSessionDataDelegate/URLSession:dataTask:didReceiveResponse:completionHandler:
You can simply not implement this method or then have to so that it allows further response.