I managed to locate the sqlite file, and its in this path now:
Library/Developer/CoreSimulator/Devices/(numbers and letters)/data/Containers/Data/Application/(numbers and letters)/Documents/
(numbers and letters) stands for a folder that would be unique to your app/computer, but would look like this: 779AE2245-F8W2-57A9-8C6D-98643B1CF01A
I was able to find it by going into appDelegate.m, scrolling down to the
- (NSURL *)applicationDocumentsDirectory
method, and NSLogging the return path, like this:
// Returns the URL to the application's Documents directory.
- (NSURL *)applicationDocumentsDirectory
{
NSLog(@"%@",[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]);
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
This will give you your unique path, making it easier for you, because it is tricky locating it with the 2 unnamed folders/strings of letters and numbers.
Swift 4.2:
let paths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
print(paths[0])
I wrote a bash script for finding the location of your Application folder and the Application data folder within a simulator, you can download it here. Once you have unzipped the script you can navigate to the folder the script is in and use it. If you call it with no options it will give you a list of installed iOS simulators. You can then use the options to find your program. Writing:
./simulatorAppFolders -s "iPad Air" -i iOS-8-0 -a <MyApp>
will find the application folder and home area folder for your app on the iPad Air iOS 8 simulator. Here's an example:
There's an update to the original script which allows you to search based on the device type using a -d option. I've also written another script which makes a series of sensibly named folders so you can find your application and application documents. This script will make a folder called iOS_SIMULATORS in your home area and you can easily navigate to your application from there.
Like James Snook I created my own script to provide easier access to the simulator apps. I think it can be handy to many people getting here, other than me.
Unlike his script, mine is not a search tool, it creates a complete (and human readable) folder structure with all the devices/runtimes, and puts soft links to the apps in them.
Run it every time you install an app in the simulator, or just run it every time you are going fetch an app for any reason (it will update, and open the devices folder in Finder).
After new updates of Xcode I found that the sql files are now stored in new folder /Library/Application Support this could be a unique situation but if you did not found the sql files in document folder search them here:
/**
Copies the sqlite, wal and shm file to the destination folder. Don't forget to merge the wal file using the commands printed int the console.
@param destinationPath Path where sqlite files has to be copied
@param persistentContainer NSPersistentContainer
*/
public static func getSqliteTo(destinationPath: String, persistentContainer: NSPersistentContainer) {
let storeUrl = persistentContainer.persistentStoreCoordinator.persistentStores.first?.url
let sqliteFileName = storeUrl!.lastPathComponent
let walFileName = sqliteFileName + "-wal"
let shmFileName = sqliteFileName + "-shm"
//Add all file names in array
let fileArray = [sqliteFileName, walFileName, shmFileName]
let storeDir = storeUrl!.deletingLastPathComponent()
// Destination dir url, make sure file don't exists in that folder
let destDir = URL(fileURLWithPath: destinationPath, isDirectory: true)
do {
for fileName in fileArray {
let sourceUrl = storeDir.appendingPathComponent(fileName, isDirectory: false)
let destUrl = destDir.appendingPathComponent(fileName, isDirectory: false)
try FileManager.default.copyItem(at: sourceUrl, to: destUrl)
print("File: \(fileName) copied to path: \(destUrl.path)")
}
}
catch {
print("\(error)")
}
print("\n\n\n ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ NOTE ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~\n")
print("In your terminal run the following commands to merge wal file. Otherwise you may see partial or no data in \(sqliteFileName) file")
print("\n-------------------------------------------------")
print("$ cd \(destDir.path)")
print("$ sqlite3 \(sqliteFileName)")
print("sqlite> PRAGMA wal_checkpoint;")
print("-------------------------------------------------\n")
print("Press control + d")
}