I am trying to download a binary file from an http website.
func saveFile(from links:[String])
{
print("CHECK START METHOD")
if (!links.isEmpty)
{
print("CHECK START DOWNLOAD ",links[0])
let url : NSURL! = NSURL(string: links[0]) /
let request : NSURLRequest = URLRequest(url: url as URL) as NSURLRequest
let session = URLSession.shared
var dataReceived = [UInt8]()
let task = session.dataTask(with: request as URLRequest) {(data, response, error) -> Void in
if (data != nil)
{
dataReceived = [UInt8](data!)
print("CHECK 1 URL: ",dataReceived.count)
}
else
{
print("ERROR: No data returned")
}
DispatchQueue.main.async
{
print("COMPLETION")
}
}
task.resume()
}
else
{
// Deal with the error
}
}
What is weird is that I do not get either " CHECK 1 URL: " or "No data returned" printed. Neither do I get into the main thread. I cannot check whether I downloaded data. Although I can see in the console:
CHECK START METHOD
CHECK START DOWNLOAD
and the link to the URL, which I can download using regular Internet browser (so the link is correct). Funny enough, the above code seems to work when dowloading a text file but not binary data.
Am I doing something wrong?
Thanks a lot!