I have checked some old post about achieving this but have not found solution yet. I want to know if possible to do this without using third party framework. If NSURLSession can be help full. I have tried as below but going as unsupported URL error:
error submitting request: Optional(Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo={NSUnderlyingError=0x7feb907f2a80 {Error Domain=kCFErrorDomainCFNetwork Code=-1002 "(null)"}, NSErrorFailingURLStringKey=ftp.name.com, NSErrorFailingURLKey=, NSLocalizedDescription=unsupported URL})
My code: let urlString = "ftp.name.com"
let fileData = NSData(contentsOfFile: xmlFile.stringValue)
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let userPasswordString = "user:password"
let userPasswordData = userPasswordString.dataUsingEncoding(NSUTF8StringEncoding)
let base64EncodedCredential = userPasswordData!.base64EncodedDataWithOptions(.Encoding64CharacterLineLength)
let authString = "Basic \(base64EncodedCredential)"
config.HTTPAdditionalHeaders = ["Authorization" : authString]
let session = NSURLSession(configuration: config)
if let percentURLString = urlString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())
{
let url = NSURL(string: percentURLString )
let request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "POST"
request.HTTPBody = fileData
print("FTP Request :\(request)")
let task = session.dataTaskWithRequest(request, completionHandler:
{
(data, response, error) in
if (error != nil)
{
print("error submitting request: \(error)")
return
}
if let httpResponse = response as? NSHTTPURLResponse
{
if httpResponse.statusCode != 200
{
print("response was not 200: \(response)")
return
}
else if httpResponse.statusCode == 200
{
print("FIle sent")
}
}
})
task.resume()
}
}