How do i test my app data persistence during development? - iphone

My question is related to the following three questions.
iPhone What happens to previous data when app is upgraded to new version
How the application upgrade works in iPhone
Preventing erasure of user data while upgrading iOS application via iTunes
Here are my 2 questions.
Am I supposed to create a "Documents" directory manually on filesystem or create just a "group" in xcode, so that when app is upgraded, data stay persistant?
How I can test the app persistance during the app development on simulator or on IPhone?
Thanks.

Neither. The Documents directory is created for you when your app is installed. You can get the path to it using:
NSString *docuDir = nil;
NSArray *list = NSSearchPathForDirectoriesInDomains(
NSDocumentsDirectory, NSUserDomainMask, YES/*expand tilde*/);
if ([list count]) docuDir = [list objectAtIndex:0U];
As #Ben said, the directory is only deleted when you explicitly uninstall the app (delete it from within Springboard). When you build and run, Xcode effectively does an in-place upgrade of your app. If everything is intact then (and there's no reason it wouldn't be), you should be fine.

When the app is upgraded your Documents folder is left intact. You can copy files into the Documents directory the first time your app is run such as a sqlite file for example, obviously only if it doesn't already exist. If you use CoreData then it will automatically store your data there. You will need to be able to run migration scripts if your data schema changes on upgrade, so keep that in mind. CoreData has migration features to take care of this for you and of course you can do it manually too.
When you run an app in the simulator it also leaves the Documents directory intact, so you have to delete the app from the simulator to simulate a fresh install. An upgrade is simulated simply by running your code since the Documents folder was created on the 'fresh install' first run.

Another way to approach this would be look at the Documents folder. If you are running your app in the simulator, then your app's Documents folder will have a path something like this:
/Users/itsaboutcode/Library/Application Support/iPhone Simulator/4.1/Applications/SOME_LONG_HEX_KEY_THAT_MAPS_TO_YOUR_APP/
If you are testing on an iDevice, then use Xcode's Organizer window's Summary tab. At the bottom there's a section called Applications. You app will be near the top of that list and it will have a toggle triangle - which you will click on. This will reveal "Application Data" which can be downloaded by clicking on downward pointing arrow to the right.
The iPhone Simulator has the advantage that the Documents folder and its contents can be modified at will. The Application Data on iDevice is read-only (except, of course, for your app at runtime).

Related

How to safely upgrade/backup an existing app that uses core data?

I have an app that I've built a few months ago and would like to continue upgrading that app.
Since that time, the app has about 6 months of data, and I would like to backup the state of this app before working on upgrading the app (I'm afraid that I will somehow delete/mess up the core data persistent storage if I install a new version of the app.) I do not remember if I got the app from the iTunes store or xCode installation.
I've backed up my iPhone using iTunes and am currently working with an app which has a different app identifier from the production app:
The live app with data I want to save is called "app"
The work in progress version of the app is called "app_test"
There are two versions of the app on the device.
I would like to see how my changes that I've created for the app_test would look on the live production app with real data.
What steps do I need to take to ensure that I can recover my app's data if something goes wrong?
Is it enough for me to change the xcode's project bundle id from "app_test" to "app" to see changes in production?
Thank you for your input, I really value that data and do not want to lose it!
First important point - do not change your "app_test bundle" identifier to "app" - this will overwrite your live app on the phone, and your data will be gone (well, you'll have to restore from backup at least...)
You should be able to use an OSX program such as 'iPhone Explorer' to browse your connected phone - find your app, and see if you can nab the .sqlite file (maybe in the documents or libray folder of your app) - copy it to your desktop.
Now you've got your live database, you have several choices. You could also nab the app_test database, and merge the contents using your favourite sql tools - or import the live .sqlite file to your app_test xcode project, and tell app_test to use that as the data source.
If you need write access to the database, you'll have to copy the live .sqlite from your app_test bundle to the documents or library folder first.

New iPhone app as update to old app, same Bundle identifier, Xcode gives error

I currently have an app in the App Store and on my device, called, for the sake of this post, MyApp1, with bundle identifier com.myname.myapp.
Over the last couple of weeks I have been rewriting the entire app from scratch, also adding lots of new features, as a new Xcode project alongside the old one. As far as iTunes Connect and iOS are concerned though, it should still be an update do the old (version of the) app.
During testing I had both apps running alongside each other on my device and in the simulator, with the new app using bundle identifier com.myname.myapp2, and everything worked fine.
But now that the app's finished, I want it to overwrite/update the old app on my device and in the simulator, to see if the code importing the data from the old app works as expected.
So, I changed the bundle identifier of the new app in its Info.plist to com.myname.myapp, and chose Build and Debug in Xcode. But instead of the new app overwriting the old app while still using the same Documents folder, so it could import the old data, as I expected it to, I got an error.
This is what I'm doing and what happened:
Simulator: Reset cache
Xcode for MyApp1: Clean All Targets
Xcode for MyApp1: Build and Run (using Release configuration)
MyApp1: Change some NSUserDefaults data using the app, changes that should later be imported by MyApp2, which uses a different data model
Xcode for MyApp1: Stop
Xcode for MyApp2: Clean All Targets
Xcode for MyApp2: Build and Run (using Release configuration)
What happens is the following error shows up 3 times, each time for a different classs in MyApp2's Console:
Unknown class [class referenced in the MyApp1 MainWindow.xib file] in Interface Builder file.
So it looks like for some reason not all files are overwritten, as the error is caused by the old version's MainWindow.xib being loaded.
(Relatively uninteresting stack frame: http://cl.ly/1F1w1J1u3t1C2U2o2D2C)
Would anyone happen to know how I can make this work, how I can have an essentially new app overwrite the old one on my device and in the simulator, so it will still use the same Documents folder?
Thanks in advance!
Things I already tried, as suggested by others, but didn't help:
Restarting Xcode
Restarting computer
Deleting build folder from Xcode project folder
Touching MainWindow.xib
Download the old app's Documents folder to your Mac using the Xcode organizer window. Then remove the old app completely, install the new one. Finally restore the Documents folder backup using the Xcode organizer.

Is there a way to delete all iPhone application data?

Update:
The app is running on the device of an ad-hoc user.
I just want to delete a single application's data.
Original question:
Is there a way to completely wipe the an iPhone application's directory easily?
I want to delete preferences, documents, caches, everything.
I'd like to do this programmatically within the app so I could distribute it to an Ad-Hoc user.
Thanks.
Have them delete the app from their phone first. That will delete the application folder. Then they can install a fresh copy with no saved files in the app's directory.
It sounds like you have the app running in the simulator and you want to send it to an ad-hoc user? Technically, ad-hoc copies of the app need to be compiled for the ARM architecture and codesigned with an ad-hoc distribution certificate - so a copy from the simulator won't do any good.
If you just want the other person to run the app in their copy of the simulator, you can go to ~/Library/Application Support/iPhone Simulator/ and start deleting stuff, though.

location of database created by the application

I have opened a database using sqlite3 in my application. When i run the application in the simulator, this database might be created and i'm playing with it. But now i need to know the location (to be accessed from ma mac) where this database is created. Surely it is not in the application folder as i'm not seeing it. I doubt it to be somewhere in the location where the simulator is installed. Anyone knowing the location. pls help me finding it.
If you're looking for the actual file on your Mac, try looking in this folder:
/Users/YOURUSERNAME/Library/Application Support/iPhone Simulator/User/Applications/
There should be a bunch of folders with long names. If you've recently built your app for the Simulator, order the folder by Date and the newest will be at the top. The folder contains a Documents folder, but you can also right-click on the app to "Show Package Contents", which might contain your database.sqlite file.

Deploying Test Resources in the iPhone Simulator

I am working on an iPhone Application that stores images in the Applications 'Document' folder.
I am currently doing the majority of my testing using the iPhone Simulator. In order to aid development I want to have some test images pre-loaded into the Document folder of the application that my application can access and display.
I have discovered that when running the application in the iPhone simulator you can access the applications file system via
/Users//Library/Application Support/iPhone Simulator/User
The application is then represented by a UID and within that is the Document folder. Placing the images in this folder and they adding their path to my applications database allows me display test data.
The problem is that each time I build and redeploy the application the old application folder is deleted and a new one is created, this means I have to copy the images to the new application folder and update the database.
Is there a way to automatically add test resources to an application when building and deploying? Should I be putting the test data in the bundle instead, and if so is there a way to not include the test data when building the actual release?
Many thanks in advance
Xcode should be smart enough to migrate anything from your 'old' UUID-based folder hierarchy to the 'new' one each time you build. Are you not seeing this move automatically?
Note: you should definitely be using dynamically generated paths here, not hard-coded. Use [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex: 0]; or [#"~/Documents" stringByExpandingTildeInPath] to get the proper (current) path to your documents folder.