func downloadAndSaveAudioFile(_ audioFile: String, completion: @escaping (String) -> Void) { //Create directory if not present let paths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.libraryDirectory, FileManager.SearchPathDomainMask.userDomainMask, true) let documentDirectory = paths.first! as NSString let soundDirPathString = documentDirectory.appendingPathComponent("Sounds") do { try FileManager.default.createDirectory(atPath: soundDirPathString, withIntermediateDirectories: true, attributes:nil) print("directory created at \(soundDirPathString)") } catch let error as NSError { print("error while creating dir : \(error.localizedDescription)"); } if let audioUrl = URL(string: audioFile) { //http://freetone.org/ring/stan/iPhone_5-Alarm.mp3 // create your document folder url let documentsUrl = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask).first! as URL let documentsFolderUrl = documentsUrl.appendingPathComponent("Sounds") // your destination file url let destinationUrl = documentsFolderUrl.appendingPathComponent(audioUrl.lastPathComponent) print(destinationUrl) // check if it exists before downloading it if FileManager().fileExists(atPath: destinationUrl.path) { print("The file already exists at path") } else { // if the file doesn't exist // just download the data from your url DispatchQueue.global(qos: DispatchQoS.QoSClass.background).async(execute: { if let myAudioDataFromUrl = try? Data(contentsOf: audioUrl){ // after downloading your data you need to save it to your destination url if (try? myAudioDataFromUrl.write(to: destinationUrl, options: [.atomic])) != nil { print("file saved") completion(destinationUrl.absoluteString) } else { print("error saving file") completion("") } } }) } } }