Last active
July 21, 2024 18:08
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("") | |
} | |
} | |
}) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thankyou so much