Skip to content

Instantly share code, notes, and snippets.

@UmairSharif99
Last active September 18, 2021 06:09
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save UmairSharif99/d3281d147607c825c846b345ffd072d7 to your computer and use it in GitHub Desktop.
Save UmairSharif99/d3281d147607c825c846b345ffd072d7 to your computer and use it in GitHub Desktop.
Function to download audio file from url and save it in documents directory
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("")
}
}
})
}
}
}
@jawaidsolangi
Copy link

Thankyou so much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment