Skip to content

Instantly share code, notes, and snippets.

@shanecowherd
Created July 16, 2019 16:41
Show Gist options
  • Save shanecowherd/57fc313e11497e24b4186fcee4e30c85 to your computer and use it in GitHub Desktop.
Save shanecowherd/57fc313e11497e24b4186fcee4e30c85 to your computer and use it in GitHub Desktop.
//https://developer.apple.com/documentation/foundation/urlsession/1411511-downloadtask
let url = URL(string: "https://www.shanecowherd.com")!
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 {
return
}
//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)
}
downloadTask.resume()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment