How to copy the file from appBundle to document directory iOS swift?
Solution:
If you want copy the file that you had already added to your bundle to the document directory then follow the code.Below i had copied the first.wav file from bundle directory to document directory.
Code:
let destPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
let fileManager = FileManager.default
//beep sound copy
let bundlePath = Bundle.main.path(forResource: "first", ofType: ".wav")
let fullDestPath = NSURL(fileURLWithPath: destPath).appendingPathComponent("first.wav")
do{
try fileManager.copyItem(atPath: bundlePath!, toPath: (fullDestPath?.path)!)
}catch{
print("\n")
print(error)
}
If you want copy the file that you had already added to your bundle to the document directory then follow the code.Below i had copied the first.wav file from bundle directory to document directory.
Code:
let destPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
let fileManager = FileManager.default
//beep sound copy
let bundlePath = Bundle.main.path(forResource: "first", ofType: ".wav")
let fullDestPath = NSURL(fileURLWithPath: destPath).appendingPathComponent("first.wav")
do{
try fileManager.copyItem(atPath: bundlePath!, toPath: (fullDestPath?.path)!)
}catch{
print("\n")
print(error)
}
Comments
Post a Comment