我正在使用swift 3.0中的一个项目,我可以获得多个网址来保存。截至目前,我已能够将单个URL下载并保存到本地文件管理器。如何根据以下模块保存多个URL。我的代码适用于单个网址,如下所示。我该怎么改变?
public static func dowanloadFile (downloadURL: String, fileName:String, onCompletion:@escaping ()->Void, onError:@escaping ()->Void?){
// Create destination URL
let documentsUrl:URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first as URL!
let destinationFileUrl = documentsUrl.appendingPathComponent(fileName + ".mp3")
print("Dest Location: \(destinationFileUrl)")
//Create URL to the source file you want to download
let fileURL = URL(string: downloadURL)
let sessionConfig = URLSessionConfiguration.default
let session = URLSession(configuration: sessionConfig)
let request = URLRequest(url:fileURL!)
let task = session.downloadTask(with: request) { (tempLocalUrl, response, error) in
if let tempLocalUrl = tempLocalUrl, error == nil {
// Success
if let statusCode = (response as? HTTPURLResponse)?.statusCode {
print("Successfully downloaded. Status code: \(statusCode)")
print("Path is :",destinationFileUrl)
print("Path is Temp :",tempLocalUrl)
}
do {
try FileManager.default.copyItem(at: tempLocalUrl, to: destinationFileUrl)
onCompletion()
print("dest :",destinationFileUrl)
return
} catch (let writeError) {
print("Error creating a file \(destinationFileUrl) : \(writeError)")
}
} else {
print("Error took place while downloading a file. Error description: %@", error?.localizedDescription ?? "error");
}
if(onError != nil){
onError()
}
}
task.resume()
}