Skip to content

Instantly share code, notes, and snippets.

Created July 16, 2019 16:41
What would you like to do?
let url = URL(string: "")!
let downloadTask = URLSession.shared.downloadTask(with: url) { (downloadedFile, response, error) in
// Make sure the temporary file exists and you have access to it
guard let downloadedFile = downloadedFile, FileManager.default.fileExists(atPath: downloadedFile.path) else {
//downloadedFile - The location of a temporary file where the server’s response is stored. You must move this file or open it for reading before your completion handler returns. Otherwise, the file is deleted, and the data is lost.
let fileData = try? Data(contentsOf: downloadedFile)
let fileString = String(data: fileData, encoding: .utf8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment