Jul 05

With this post I will show how to copy a file added to our project to the documents folder of our application. The idea is to be able to initialize in a fast and easy way the database, some configuration files or any similar file that we must save in the documents folder because it will be change by the user during runtime. That way we don’t have to create them from the code.

First, we need to add the file to our project, add it to the “Resources” folder of your project so it is accessible from the code of your application.

Once we have the file in the project, we can add the code to copy the file to the documents folder, add the next code to your AppDelegate, you can create a new method for initialization purpose or add it directly to the AppDelegate init.

// Copy init.sqlite from resources to the Documents folder in bundle
NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"data.sqlite"];
if(![[NSFileManager defaultManager] fileExistsAtPath:storePath])
	NSString *filePath = [[NSBundle mainBundle] pathForResource:@"init" ofType:@"sqlite"];
	NSData *myData = [NSData dataWithContentsOfFile:filePath];
	if (![myData writeToFile:storePath atomically:YES]) {
		NSLog(@"ERROR writting the starting database");

In the code we use a file, init.sqlite, as an initial database for our application and we save the same file under the name data.sqlite in the documents folder of our application. Notice that we use an if to check if that file already exists, that way this code will only be executed the first time we launch our application, this is so we don’t delete any new data the user may have introduced. If during your testing you want to get the initial database again, delete the application from your iPhone or from the iPhone Simulator.

The method applicationDocumentsDirectory is the following function, returning the path to the documents folder.

 Returns the path to the application's Documents directory.
- (NSString *)applicationDocumentsDirectory {
	return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
VN:F [1.9.8_1114]
Rating: 5.0/5 (1 vote cast)
VN:F [1.9.8_1114]
Rating: +1 (from 1 vote)
Tagged with:
Jun 13

I don’t know if it has ever happend to you that to clean your iPhone Simulator instead of deleting app by app, you did a reset and found out that all pictures had dissapeared XD, what did you expect ;).

Well, if you did a reset willingly, by error or to clean all the apps and data that is created while you test and test your apps, here there are a couple of tricks to add new pictures:

Photos we have on files:

To add pictures from your own files you only have to copy them into:

~/Users/<usuario>/Library/Application Support/iPhone Simulator/3.1.3/Media/DCIM/100APPLE

Where <usuario> is your user name, and maybe you have to change too the SDK version you are working with.

Note: I have seen different routes around internet, the idea is that you have to be in the iPhone Simulator folder ~/Library/Application Support/iPhone Simulator/… and add the images to Media/DCIM/100APPLE, i this last folder doesn’t exist you must create it.

Variant: You can also open Safari in your simulator and drag file by file to the webpage, then hold left click on the image till you  get the option to save it. It will be added to your library.

Pictures from internet:

In case you want to add new images from internet, for example google images, you only need to do as before, click and hold on the image you want to safe in Safari to get the option to save it.

VN:F [1.9.8_1114]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.8_1114]
Rating: 0 (from 0 votes)
Tagged with:
preload preload preload