我的项目要求我下载 .pdf 文件,将其保存在 iPad 本地,然后制作电子邮件并发送。看起来很直接。
这是执行下载的代码:
func downloadFile() {
let urlString = <private url>
let url = URL(string: urlString)!
let downloadTask = URLSession.shared.downloadTask(with: url) {
urlOrNil, responseOrNil, errorOrNil in
// check for and handle errors:
// * errorOrNil should be nil
// * responseOrNil should be an HTTPURLResponse with statusCode in 200..<299
guard let fileURL = urlOrNil else { return }
do {
let documentsURL = try
FileManager.default.url(for: .documentDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: false)
let savedURL = documentsURL.appendingPathComponent(url.lastPathComponent)
try? …Run Code Online (Sandbox Code Playgroud)