Copy data file from Xcode to iPad sandbox - xcode

I created an app in Xcode and added a text file for the app to open and display.
How can i get this text file into the iPad sandbox documents directory for testing?

There are a few things you need to do:
Create a path to your Documents directory
Make sure your text file is in your app bundle (Drag/drop into Xcode should be fine)
Copy that text file by name from the app bundle to the Documents directory
Try this:
// Documents path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
// Destination path
NSString *fileInDocumentsPath = [documentsPath stringByAppendingPathComponent:#"name-of-your-file.txt"];
// Origin path
NSString *fileInBundlePath = [[NSBundle mainBundle] pathForResource:#"name-of-your-file" ofType:#"txt"];
// File manager for copying
NSError *error = nil;
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager copyItemAtPath:fileInBundlePath toPath:fileInDocumentsPath error:&error];

Related

Save an image from photo library to Main Bundle in app Xcode

is it possible to save a image I have picked form the photos library to the Main Bundle of my app? I thought I read somewhere that you can't write to the main bundle of an app in Xcode. If this is not true and it is possible how would I go about doing it.
Use this code to save image from photo Library to Main Bundle
- (void)saveImage:(UIImage*)image withImageName:(NSString*)imageName {
NSData *imageData = UIImagePNGRepresentation(image); //convert image into .png format.
NSFileManager *fileManager = [NSFileManager defaultManager];//create instance of NSFileManager
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //create an array and store result of our search for the documents directory in it
NSString *documentsDirectory = [paths objectAtIndex:0]; //create NSString object, that holds our exact path to the documents directory
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.png", imageName]]; //add our image to the path
[fileManager createFileAtPath:fullPath contents:imageData attributes:nil]; //finally save the path (image)
}

Reading files from Documents directory using xcode

with reference to my own question,
UITable is not getting populated with NSMutableArray here i got all the file names into a table. And these files where stored in 'Resources' folder. Now i need to fill the same table from the files stored in a subfolder say 'sample' which is in documents folder of ios simulator.
Earlier i did this with the following code
files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:
[[NSBundle mainBundle] bundlePath] error:nil];
search_results_array = [files filteredArrayUsingPredicate:
[NSPredicate predicateWithFormat:#"self BEGINSWITH[cd] 'h'"]];
here both files and search_results_array are arrays. Now the reading is not from resources. How can i edity it? anybody please help.
Use the following:
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //Library directory is not accessible via iTunes, Document directory is
NSString *docDirectory = [paths objectAtIndex:0]; //2
path = [docDirectory stringByAppendingPathComponent:yourString]; //3
files = [[NSDictionary alloc] initWithContentsOfFile: path];
Assuming files is an NSDictionary.

Problems adding data to a plist file

I've been trying to write data back to a pre-defined plist file (data.plist) in my bundle. Using the code below I call the routine 'dictionaryFromPlist' to open the file and then call 'writeDictionaryToPlist' to write to the plist file. However, no data gets added to the plist file.
NSDictionary *dict = [self dictionaryFromPlist];
NSString *key = #"Reports";
NSString *value = #"TestingTesting";
[dict setValue:value forKey:key];
[self writeDictionaryToPlist:dict];
- (NSMutableDictionary*)dictionaryFromPlist {
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"data" ofType:#"plist"];
NSMutableDictionary* propertyListValues = [[NSMutableDictionary alloc]
initWithContentsOfFile:filePath];
return [propertyListValues autorelease];
}
- (BOOL)writeDictionaryToPlist:(NSDictionary*)plistDict{
NSString *filePath = #"data.plist";
BOOL result = [plistDict writeToFile:filePath atomically:YES];
return result;
}
The code runs through successfully and no error is thrown but no data is added to my plist file.
You can not write to your bundle, it is read only. If your case though, you are writing to a relative path, not to the bundle.
I'm not sure what the default working directory is for iOS apps. It is best to use absolute paths. You should be writing to the documents/cache directory. Something like this will get the path for you:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
Then just grab the lastObject and prepend that to your file name.
As mentioned by #logancautrell you can not write in mainbundle, you can save your plist in the app documents folder, you could do so:
NSString *path = #"example.plist";
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count]> 0)? [paths objectAtIndex: 0]: nil;
NSString *documentPath = [basePath stringByAppendingPathComponent:path] // Documents
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL checkfile = [fileManager fileExistsAtPath: documentPath];
NSLog(#"%#", (checkFile ? #"Exist": #"Not exist"));//check if exist
if(!checkfile) {//if not exist
BOOL copyFileToDoc = [yourDictionary writeToFile:documentPath atomically: YES];
NSLog(#"%#",(copyFileToDoc ? #"Copied": #"Not copied"));
}

How to specify working directory for sqlite3_open

Working in xCode everything works OK if I execute by double-clicking the project down in the working directory but not from xCode itself doing a Build and Run. Then the database isn't being found correctly.
How do I modify...
sqlite3_open("Airports.sqlite", &db);
so it can find Airports.sqlite in the current working directory?
Copy your database to your applications directory and after that you can use your database :)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *dbPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:#"Airports.sqlite"];
BOOL success = [fileManager fileExistsAtPath:dbPath];
if (!succes){
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"Airports.sqlite"];
BOOL success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
if (!success)
NSAssert1(0, #"Failed to create writable database file with message '%#'.", [error localizedDescription]);
else
NSLog(#"Succesfully created writable database");
}

creating copy of file using file manager is not working for mac app

I have a file in one of the folder in my documents folder .I want to create a copy of that file in some other folder with some other name.The code I am using is
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSString * filePath = [NSHomeDirectory() stringByAppendingPathComponent:
[NSString stringWithFormat:#"Documents/test/test1/%#%#%#",str,currentaudioTobPlayed,#".mp3"]];
NSString * filePath2 = [NSHomeDirectory() stringByAppendingPathComponent:
[NSString stringWithFormat:#"Documents/test/test1/"]];
NSLog(#"filePat %#",filePath);
NSLog(#"filePath2 %#",filePath2);
NSLog(#"filePath2 %#",fileManager);
[fileManager copyItemAtPath:filePath toPath:filePath2 error:NULL];
[fileManager release];
But this code is not working.Please help!
In:
NSString * filePath2 = [NSHomeDirectory() stringByAppendingPathComponent:
[NSString stringWithFormat:#"Documents/test/test1/"]];
you’re not specifying the destination file name: ~/Documents/test/test1 is a directory.
You could do this instead:
NSString *sourceName = [NSString stringWithFormat:#"%#%#.mp3",
str, currentaudioTobPlayed];
// Choose the destination file name
NSString *destName = [NSString stringWithFormat:#"Copy of %#", sourceName];
NSString * filePath = [NSHomeDirectory() stringByAppendingPathComponent:
[NSString stringWithFormat:#"Documents/test/test1/%#", sourceName]];
NSString * filePath2 = [NSHomeDirectory() stringByAppendingPathComponent:
[NSString stringWithFormat:#"Documents/test/test1/%#", destName]];
// Replace Documents/test/test1 with another directory under home,
// or replace the whole string with an entirely different directory

Resources