Skip to content

Instantly share code, notes, and snippets.

@kyleturner
Created January 22, 2019 02:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyleturner/199b69eb2d26797743f682118b0eeb07 to your computer and use it in GitHub Desktop.
Save kyleturner/199b69eb2d26797743f682118b0eeb07 to your computer and use it in GitHub Desktop.
#ios #swift #files #bundle
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