Last active
December 9, 2018 22:29
-
-
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).
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 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