Skip to content

Instantly share code, notes, and snippets.

@roman-wb
Last active December 9, 2018 22:29
Show Gist options
  • Select an option

  • Save roman-wb/23f10bfc0dbcce3b8b24aefd18f3cd1f to your computer and use it in GitHub Desktop.

Select an option

Save roman-wb/23f10bfc0dbcce3b8b24aefd18f3cd1f to your computer and use it in GitHub Desktop.
Copy app bundle file (read-only) to app documents (read-write, database etc).
func copyDatabaseIfNeeded() {
let fileManager = FileManager.default
guard let documentsUrl = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first else { return }
let finalDatabaseUrl = documentsUrl.appendingPathComponent("sample.db")
do {
if !fileManager.fileExists(atPath: finalDatabaseURL.path) {
if let dbFilePath = Bundle.main.path(forResource: "sample", ofType: "db") {
try fileManager.copyItem(atPath: dbFilePath, toPath: finalDatabaseUrl.path)
} else {
print("Database file found at app bundle")
}
} else {
print("Database file found at path: \(finalDatabaseUrl.path)")
}
} catch {
print("Unable to copy database file: \(error)")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment