Open sqlite database file inside iphone simulator? - iphone

I'm having a hard time trying to understand a problem in my iphone app.
I have a set method in my sqlite database that looks like it is working fine. I do a NSLog when I am setting the value and it is correct. It's a string with the value "2010-10-10 12:13:14".
When I use the get method on the same data, it turns out that the value is not correct. It returns 1.
Is there any way to open the database file that is inside the iPhone simulator so I can see what is the actual data stored in the simulator database?
Is there any way to sniff the sqlite calls between my simulator and the actual file? Something like Charles, or Service Capture?
Many thanks!

Since IOS 8 they put it in a different folder:
Library/Developer/CoreSimulator/Devices/(numbers and letters)/data/Containers/Data/Application/(numbers and letters)/Documents/
Check this answer for more info

If you want to view the database that you have created,
Open finder -> press SHIFT + Command + G -> "~/Library/Application Support/iPhone Simulator" then go.
Open 5.0 (as per your version of simulator)-> Application-> select the pgm folder
-> Documents
Then you can see the database

Update: I made a tool to automate this
Both for Android and iOS, it's available here
Old question, but Apple seem to keep change the logic...
On macOs Sierra you can find the database location by issuing the following commands (the simulator must run during the process!)
ps aux | grep 'CoreSimulator/Devices'
The result should list the path to the simulator, for example:
/Users/user1/Library/Developer/CoreSimulator/Devices/25C1F5DA-E528-4BC2-B684-A92D382553AC/data/var/run/launchd_bootstrap.plist
Navigate to the now found simulator folder:
cd /Users/user1/Library/Developer/CoreSimulator/Devices/25C1F5DA-E528-4BC2-B684-A92D382553AC/Data/Application/
This folder contains all the simulator applications
From here if you know the database name, just find it, like so:
find ./ -type f -name 'my_db.db’
And viola! You have the database path :)

You can refer to the following link below,You can definitely look at the database stored inside the simulator.
How to view the data in sqlite file running in iphone application?
Cheers

For the 6.1 simulator, the location is different.
Start at:
~/Library/Application Support/iPhone Simulator/6.1/Applications/
Open the folder that represents the application on your simulator. The name will look similar this:
0951F670-1DAC-9A39-450A-B8EEFFC0FFE1
If you have multiple folders, then you can click into each folder and look at the app icon until you find the app that you are looking for. Alternatively, uninstall all other apps from the simulator except the one you are interested in.
From inside this folder, go here:
Library/Application Support/<APP_NAME>/<APP_NAME>.sqlite
The total path should look like this:
~/Library/Application Support/iPhone Simulator/6.1/Applications/<APP_ID>/Library/Application Support/<APP_NAME>/<APP_NAME>.sqlite

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(#"path is %#",documentsDirectory);
The above code will print the path of ur database and copy it. Go to finder and choose go to folder from menu (Shift+Win+G) and paste the path here, it will give you the exact path of core data file. Keep this window open.
Next step Go to Mozilla firefox click on menubar choose Tools -> SQLite Manager. It will open new window. In new window click on open file and drag that path of coredata file in current window and click on open.
Note - You must have Installed SQLite Manager in your Mozilla firefox.

For 2019 and 2020 answer: ⚠️
This is easy. Use a free-opensourced application called Simsim (https://github.com/dsmelov/simsim).
Read the README and there's a direct download link there.
Choose Finder, and from there, you can find the .sqlite db file in the Documents or use the search function of the Finder targeting your application folder.

Xcode 12.5+
Find the path to .sqlite
let fileManager = FileManager.default
let databasePath = try fileManager.url(
for: .applicationSupportDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: true
)
.appendingPathComponent("yourDatabaseName.sqlite")
.path
go to the file
Open finder -> press SHIFT + Command + G -> past the "file path" then go.
view sqlite file and query it
https://inloop.github.io/sqlite-viewer/

Related

How to find the path of sqlite.sql file and print it?

I know this question already answered before but when I tried to print the path for the sqlite file of my application as below I got the path but could not go to it in finder.
I used below code to get the path
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last! as String
print(paths)
The path I got is as below:
/var/mobile/Containers/Data/Application/5412CDDE-8D40-46B2-A61D-65065796F7CE/Documents
when I try to copy and paste it in GOTO it can not find it
please advise me
In case you are using NSPersistantContainer, you get the URL(s):
persistentContainer.persistentStoreCoordinator.persistentStores.first!.url!
when I try to copy and paste it in GOTO it can not find it
that path is on the Simulator and not on the local Finder.
Duplicate of: Access files in /var/mobile/Containers/Data/Application without jailbreaking iPhone
or
Duplicate of: How to view the data in sqlite file running in iphone application?
How to find the path of sqlite.sql file and print it?
accessing your sqlite database is described here:
Copy Sqlite DataBase in Swift does not work properly
Use this in your swift class to get the location of sqlite database for your application running in iOS simulator.
let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
print(urls[urls.count-1] as URL)
You will need SQLite database browser to look after the database. We can only locate sqlite database file only for application running in simulator but not in the iPhone directly.

openURL on local file in temporary directory does nothing

I'm saving a downloaded PDF file to a temporary directory on my device, and have the URL for the resultant file:
file:///Users/colinbasnett/Library/Developer/CoreSimulator/Devices/A057DDAD-B116-424B-8383-442321530EEC/data/Containers/Data/Application/A0AEF93A-5B1D-4CB4-B39F-F6DFECEDD9E9/tmp/FF5C09A9-45CD-454E-B55A-4F5CEBFEBC7F-24875-000014B657DC6436/23659.pdf
Where fileURL is a valid NSURL object representing the path above, I call this:
UIApplication.sharedApplication().openURL(fileURL)
and nothing happens.
Despite this, the following call returns true:
UIApplication.sharedApplication().canOpenURL(fileURL) // returns true
Ideally this would open in Safari or whatever the preferred browser application is. Interestingly enough, I can manually open Safari, paste that directory in the address bar and it is able to display the PDF.
I'm using Swift 2.2 (can't switch to 3 yet because dependencies have not been upgraded).
I think the problem is that openURL() wants to launch a separate application, but you're passing a file: URL that points to a file in your sandbox. I suspect UIDocumentInteractionController might be more what you're after.

Can't Find iPhone Simulator's SQLite Database : Using Magical Record

In my application's documents folder, I do not find my SQLite database
Library/Application Support/iPhone Simulator/6.1/"random app
id"/Documents/
^This folder is empty
MagicalRecord seems to be working, I just have no idea how to find the database.
I use the following to set up Magical Record
[MagicalRecord setupAutoMigratingCoreDataStack];
So where does MagicalRecord store its database?
MagicalRecord stores, by default, all data stores in the following location:
{App Folder}/Library/Application Support/{App Name from Info.plist}/{storeName.sqlite}
This is easily accessible from the simulator as well as documents.
You can log your SQlite file location using the following NSPersistentStore MR addition :
[NSPersistentStore MR_urlForStoreName:[MagicalRecord defaultStoreName]]
If you know the name of the sqlite file then just do a search in OSX for that file to find the directory. Otherwise the file was never created.
Make sure you are setting up the CoreData stack correctly as per the documents.
+ (void) setupCoreDataStackWithAutoMigratingSqliteStoreNamed:(NSString *)storeName;
Swift + Xcode 7 + simulator 9.0
Please go to your AppDelegate.swift
change
MagicalRecord.setupCoreDataStackWithStoreNamed("yourDBName.sqlite")
to
MagicalRecord.setupCoreDataStackWithAutoMigratingSqliteStoreNamed("yourDBName.sqlite")
Now you can locate the yourDBName.sqlite at
/Users/userName/Library/Developer/CoreSimulator/Devices/"random app id"/data/Containers/Data/Application/"random app id"/Library/Application Support/ProjectName
There is a useful tool: simPHolders2 (http://simpholders.com)
simPHolders easy to access all application folders

Can I make my iPhone app start with a file in its Documents directory?

During development, I'd like to have an XML file in my iPhone app's Documents directory when it starts. Is this possible? Having read this answer, I tried adding a Run Script build phase, but I can't find an environment variable that points to the application's home directory after installation. To be clear, the directory I want to move the file to is here, in the case of a Simulator:
/Volumes/Stuff/Users/johnDoe/Library/Application Support/iPhone Simulator/User/Applications/118086A0-FAAF-4CD4-9A0F-CD5E8D287270/Documents
Or here, in the case of a device:
/var/mobile/Applications/30B51836-D2DD-43AA-BCB4-9D4DADFED6A2/Documents
It won't do just to copy the file there manually, because I want to send the source code to someone else so they can see it, and I don't want them to have to do any set-up before they can run it. If what I'm asking isn't possible, then I'll look at ways to move the file programmatically after the app has launched, but I thought I'd check with you guys for a neater way first.
Do you need to have the file in the Documents directory, or do you just need to have access to it from within the app? In Xcode your current target should have a "Copy Bundle Resources" build phase. Add your .xml file to this build phase and you'll be able to access the file at any point in your code with the following call:
[[NSBundle mainBundle] pathForResource:#"my_file" ofType:#"xml"]

How to view data stored in Core Data?

I'm creating a Core Data model for my application. I would like to be able to look inside it to see what I have stored in there.
Is there an easier way than searching for the backing store (mine should be SQLite) and reading it from there? Doesn't seem very Apple-esque.
Once your app has run in the simulator and created the persistent store file, you can find the file in your app's Documents directory.
Your app data will be folder inside (~ is your home directory):
~/Library/Developer/CoreSimulator/<device>
In versions prior to XCode 6, the path was:
~/Library/Application Support/iPhone Simulator/User/Applications/
I sort by "Date Modified" to find the app that I just built.
For viewing a SQLite file, check out Base and/or Core Data Editor.
You can also use FireFox's Add-on tool SQLite Manager (FREE). I use it and it works well.
If you are using iOS 10 & Swift get it into your AppDelegate.swift:
let container = NSPersistentContainer(name: "***")
print(container.persistentStoreDescriptions.first?.url)
Then you'll see something like that:
Optional(file:///Users/<SomeUser>/Library/Developer/CoreSimulator/Devices/956F0003-5DA4-4588-97C1-A9A83767F341/data/Containers/Data/Application/9765FDE1-6971-4706-987B-96FBD3F462BF/Library/Application%20Support/***.sqlite)
You can try a little free tool called CoreDataPro. Pretty nice. Here
Core Data Lab is a dedicated Core Data viewer that offers everything to view and analyze the Core Data database of your app, like automatic search for databases, data change tracker, simulator browser, a predicate editor, data editor, a built-in web and image content viewer, and much more.
Info page: https://betamagic.nl/products/coredatalab.html
Free 14-trial: https://betamagic.nl/downloads/Core%20Data%20Lab%20Trial.zip
Diclaimer: I'm the creator of this tool.
I found a free version here:
http://sourceforge.net/projects/sqlitebrowser/files/sqlitebrowser/2.0%20beta1/sqlitebrowser_200_b1_osx.zip/download
Update to this thread, The iphone simulator location has been moved to a different location in Xcode7 and Xcode8. It is now in:
~/Library/Developer/CoreSimulator/...
where ~ is your home directory.
Any of the previous solutions will still work, but you won't be able to find the correct folder if you use the previous links.
Also try sqlite database browser. its free and reflects that fact. very useful though!
cheers.bo
I created a macOS app to browse your Core Data content. It hides Core Data implementation as you see when you use a SQLite browser. Also you can list all your latest run apps and simulators, with this you can easily find the app you are looking for. More info here:
http://www.microedition.biz/simMagnifier
It has other features like access Simulator's folders such as Documents, Application Support, etc. You can check the content of NSUserDefaults too.
The app is not free sorry! but at least you have a 7 days trial.
Here is the full path, apparently it changed since gerry3's answer
~/Library/Application Support/iPhone Simulator/<Simulator>/Applications/<ApplicationID>/Documents/<NAME>.sqlite
So a correct path would like this:
~/Library/Application Support/iPhone Simulator/7.1-64/Applications/C1F3C2E5-987A-4DF7-91EF-955AE6E52621/Documents/TestApp.sqlite
3 Easy Steps
install liya application, its free, Size 3.1 MB, so installs in 1 minute.
Link https://itunes.apple.com/us/app/liya/id455484422?mt=12.
Open Finder, Press "Ctrl + G" OR "CMD + Ctrl + G", Paste ~/Library/Developer/
In Developer Folder, search for your Core Data model name (eg. MyProjectModel).
Open MyProjectModel.d file Or 'MyProjectModel.sqlite' using liya. In liya, select a table, Click on "Run SQL Command".
Done.