Swift + SQLite: How to Check the Database File Location in iOS Apps
In this article, we'll explain how to find the location of the SQLite file used in an iOS app with Swift.
Finding the SQLite File Path in iOS Apps
To check the location of the SQLite file used in an iOS app, you can use the NSSearchPathForDirectoriesInDomains() function.
The NSSearchPathForDirectoriesInDomains() function returns a list of paths for the specified directory based on the given arguments.
func NSSearchPathForDirectoriesInDomains(
_ directory: FileManager.SearchPathDirectory,
_ domainMask: FileManager.SearchPathDomainMask,
_ expandTilde: Bool
) -> [String]
Since SQLite files are stored in the user's Documents directory, set FileManager.SearchPathDirectory to .documentDirectory and FileManager.SearchPathDomainMask to .userDomainMask (the user's home directory).
Let's add the following code to viewDidLoad() in the sample from Swift - Insert Data into SQLite (INSERT) and run it in the simulator:
print(NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true))
["/Users/user1/Library/Developer/CoreSimulator/Devices/1ECB918D-0D16-47C1-980D-2620697B6C9F/data/Containers/Data/Application/812FBEC1-1FA8-414A-815A-9CCC70F4D509/Documents"]
Opened connection to database
Insert success
The path to the user's Documents directory where the SQLite file is stored has been printed.
Opening the SQLite File Path in Finder on Mac
Next, let's open the path we just retrieved in Finder on your Mac.
From the Finder menu, select Go > Go to Folder...
Copy and paste the path into the field and press the return key.
The specified folder opens, and you should see the SQLite file inside.
That's it — we've explained how to find the SQLite file location used in iOS apps with Swift.