Hi all,
I have a question. I got a webview (hidden) and im logged in a company website.
all good so far.
But i want to download a file. But its not as simple as https://www.apple.com/mypdf.pdf.
The url is for example like https://www.apple.com/getfile?monitor=1234567. In a normal browser it will download a mp3 file.
I use this code in FileDownloader.swift:
import Foundation
class FileDownloader : NSObject, URLSessionDownloadDelegate {
var url : URL?
// will be used to do whatever is needed once download is complete
var yourOwnObject : NSObject?
init(_ yourOwnObject : NSObject)
{
self.yourOwnObject = yourOwnObject
}
//is called once the download is complete
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL)
{
//copy downloaded data to your documents directory with same names as source file
let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
//FileManager.default.
let destinationUrl = documentsUrl!.appendingPathComponent(url!.lastPathComponent)
let dataFromURL = NSData(contentsOf: location)
dataFromURL?.write(to: destinationUrl, atomically: true)
//now it is time to do what is needed to be done after the download
//yourOwnObject!.callWhatIsNeeded()
}
//this is to track progress
private func URLSession(session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64)
{
}
// if there is an error during download this will be called
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?)
{
if(error != nil)
{
//handle the error
print("Download completed with error: \(error!.localizedDescription)");
}
}
//method to be called to download
func download(url: URL)
{
self.url = url
//download identifier can be customized. I used the "ulr.absoluteString"
let sessionConfig = URLSessionConfiguration.background(withIdentifier: url.absoluteString)
let session = Foundation.URLSession(configuration: sessionConfig, delegate: self, delegateQueue: nil)
let task = session.downloadTask(with: url)
task.resume()
}
}
and in my viewcontroller i use this to start the download.
let strURL ="https://www.apple.com/getfile?monitor=1234567"
let url = URL(string: strURL)
FileDownloader(url! as NSObject).download(url: url!)
It works but i get one file named getfile without an extension in the documents folder and its always 10.8 bytes.
How can I get the response from the website and download the mp3 file.
i hope someone can point me in the right direction.