Created
January 22, 2019 02:47
-
-
Save kyleturner/199b69eb2d26797743f682118b0eeb07 to your computer and use it in GitHub Desktop.
#ios #swift #files #bundle
This file contains 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
public func copyFileToUserDocuments(forResource name: String, | |
ofType ext: String, | |
bundle: Bundle? = nil) throws { | |
let bundle = bundle ?? Bundle.main | |
guard | |
let bundlePath = bundle.path(forResource: name, ofType: ext), | |
let destPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, | |
.userDomainMask, | |
true).first else { return } | |
let fileName = "\(name).\(ext)" | |
let fullDestPath = URL(fileURLWithPath: destPath) | |
.appendingPathComponent(fileName) | |
let fullDestPathString = fullDestPath.path | |
if !fileExists(atPath: fullDestPathString) { | |
try copyItem(atPath: bundlePath, toPath: fullDestPathString) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment