I'm using this code to read data from a local (in the XCode project) .plist file.
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"GameData.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: path])
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:#"GameData" ofType:#"plist"];
[fileManager copyItemAtPath:bundle toPath: path error:&error];
}
gameData = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
And it worked great the start, but now I find when I step through the code in the simulator that my game is using a version of the .plist file that existed right at the start, and the new fields I've set up in my new GameData.plist file is not appearing.
I presume that's because it doesn't get the data again if the file already exists? but then how do I get the new version of the .plist file? I tried removing the if statement, but I get a runtime error saying the file already exists.
It sounds like you need to setup a version system. But, the only reason to copy a file from the bundle to the documents folder is so you can edit it, so you need to think about how to merge the updated file in the bundle and the users additions in the documents folder.
Once you have a plan for merging the files, then you can change your if statement so it doesn't just check if the file already exists (if it does, you need to delete it before you can replace it) but also considers the version. You may want to store the currently copied version number in user defaults and add a version number to the file in the bundle. You may also want to keep the user modified data in a different file and use that as overrides.
Related
My app uses iTunes File Share. I used the code to delete a single file:
It worked the first time. On the second try, however, iTunes showed a empty share directory. It turns out all data files are gone. Can I recover those data files from the iPad? Thanks
- (void) deleteFileFromDisk: (NSString*) fileName {
if([self fileExists: fileName]) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) ;
NSString *documentsDirectory = [paths objectAtIndex: 0];
NSString* theFile = [documentsDirectory stringByAppendingPathComponent: fileName];
NSError *error;
[[NSFileManager defaultManager] removeItemAtPath: theFile error: &error];
There's no "restore" feature on the iPad. But in all probability there's nothing to worry about.
There's nothing about your code that would delete multiple files. It would delete just that file from the Documents directory that you supplied the name of as fileName. If you didn't call deleteFileFromDisk: multiple times, you didn't delete multiple files.
Perhaps at some point you deleted the app. That would delete its entire sandbox and thus would take with it anything in the Documents directory. That sort of thing is perfectly normal during repeated testing.
I'm working with a sqlite database application using FMDatabase.
I want to update a table of a database located in the main bundle
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:db_name];
with data in a Database located in the Documents Directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex:0];
NSString *db_Path = [docsPath stringByAppendingPathComponent:db_name];
I tried many times to execute an UPDATE query on the "default" database but it says it is locked. So I thought about coping it in Documents Directory with the name db_name_cp and working over it.
NSString *cpPath = [documentsDirectory stringByAppendingPathComponent:#"db_name_cp.sqlite"];
success = [fileManager fileExistsAtPath:cpPath];
if (success){
//remove the existing one
[fileManager removeItemAtPath:cpPath error:&error];
}
//create the copy of the "main bundle" database
[fileManager copyItemAtPath:default_database.dbPath toPath:cpPath error:&error];
Then i tried to execute the UPDATE over the copied database but if I execute a SELECT over it, it is not updated and if I run the query on phpMyAdmin or SQLiteManager it updates the records.
Any idea? Maybe I'm wrong in coping them over the device directories.
Many thanks
You can't make changes to anything in the app, this obviously includes the app's mainBundle, it is readonly and signed.
The Documents directory is fine, that it the correct directory to use.
If you need to merge then merge into the Documents directory DB. Or copy the bundle DB to the Documents directory on first launch and then merge into it.
I'm using NSFileManager and i'm trying to copy a file to path "/var/mobile/Applications/7AC2295E-2775-41EA-B017-AB4048A09F0C/Document" the file will copy fine.
but the path of "7AC2295E-2775-41EA-B017-AB4048A09F0C" is randomly changed in every time i delete and install the app again. So, is there a way to get the correct path of my app or search for file name, If file exist then replace\delete..etc the file? thanks alot.
The proper way to get access to the Documents directory in your app's sandbox is:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = paths[0];
Then you create your path:
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"filename.ext"];
You don't have to use the absolute path, use something like
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
[[NSFileManager defaultManager] copyItemAtPath:[[NSBundle mainBundle]pathForResource:#"test" ofType:#"txt"] toPath:[documentsDirectory stringByAppendingPathComponent:#"test.txt"] error:nil];
This demonstrates copying the file test.txt from the apps bundle to the documents directory.
Do something like this to create a file test.txt
[[NSFileManager defaultManager] createFileAtPath:[documentsDirectory stringByAppendingPathComponent:#"test.txt"] contents:[NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://a-cstudios.com/text.json"]] attributes:nil];
EDIT
If your using this and want to use it on a jailbroken device, to put something in the sandbox is the same as I said before, so you still don't have to use the UUID. If you want to put something in a system directory, that is when you would use something like /usr/include/, or wherever you want to. To put something in another apps sandbox, you would have to use the complete path, and you would have to know the UUID, as thats not part of your sandbox. So, unless you're copying a file to an app that you made, you have to use the complete path.
If your application needs to write a file to its Documents directory.
use something like :
NSString *filePath = NSHomeDirectory();
then append your actual file name to filePath.
then use filePath in your FileManager calls.
I want to distribute a populated database with my app. I am trying to implement the SQLitePlugin, but as the page says there is not any documentation for how to do it on iOS. I tried to implement this outdated steps but without any luck. Is there anyone who did this?
I found a solution at least it works for me so here is :
I used and followed the tutorial in the link you mentioned : http://gauravstomar.blogspot.fr/2011/08/prepopulate-sqlite-in-phonegap.html
Be sure to copy and past the good database.db and 000...001.db (the good one are either in /Caches and /Caches/file_0 folders or in /WebKit and /WebKit/file__0, i don't remember so try one then the other if it doesn't work). then i pasted them in my resources' project directory.
I modify the two lines :
NSString *masterPath = [libraryDir stringByAppendingPathComponent:#"WebKit/Databases/"];
NSString *databasePath = [libraryDir stringByAppendingPathComponent:#"WebKit/Databases/file__0/"];
by the following lines :
NSString *masterPath = [libraryDir stringByAppendingPathComponent:#"Caches/"];
NSString *databasePath = [libraryDir stringByAppendingPathComponent:#"Caches/file__0/"];
Remove your application from your simulator/device. And then run.. it works, my database is pre-populated.
I put the database in the Resources folder and placed this code in MainViewController.m.
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *databasePath = [documentsDirectory stringByAppendingPathComponent:#"this.db"];
if ([fileManager fileExistsAtPath:databasePath] == NO) {
NSString *resourcePath = [[NSBundle mainBundle] pathForResource:#"this" ofType:#"db"];
[fileManager copyItemAtPath:resourcePath toPath:databasePath error:&error];
}
The following comes from a blog post - but as I've been slightly spanked for posting links before I'm going to quote from it. I'll post the URL at the end though and I recommend you go there for a full explanation and some follow up comments. This help comes credit Scott Buckel.
1) Use this plugin: https://github.com/chbrody/Cordova-SQLitePlugin/
2) Copy your sqlite db file to /assets folder of PhoneGap.
3) Then, follow instructions here: http://gauravstomar.blogspot.com/2011/08/prepopulate-sqlite-in-phonegap.html to copy the file to native storage. Change this.copy("Databases.db","/data/data/"+pName+"/app_database/");
3a) Instead of Databases.db, use your database filename.
3b) Instead of app_database, use "databases"
3c) You'll probably want to delete the file from /assets, since it is duplicated and no longer needed. My app was double the size it needed to be.
4) (Not sure if this step is necessary, I'm getting out of the office for the day). Edit SQLitePlugin.java
4a) Lines 176, I edited 1==1 so that if statement is always executed. Line 179 I changed this.setStorage(appPackage, false); to this.setStorage(appPackage, true);
5) You can then use the following command to open the DB and use it as any other PhoneGap database
5a) var db = window.sqlitePlugin.openDatabase("[full_database_name_including_extension]", "1.0", "PhoneGap Demo", 200000);
Full blog post: http://www.raymondcamden.com/index.cfm/2012/7/27/Guest-Blog-Post-Shipping-a-populated-SQLite-DB-with-PhoneGap
How to create a file and save it as a text file in iPad through the application?. If I had to read that file at a later stage how to specify the absolute path. Could some one help on this.
Is it possible to display the text file created using the iPad app in the device homescreen, so that user's will be able to modify it.
To Write a file to documents directory use following code, (you may need to do some syntax correction as not tested)
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir =[documentPaths objectAtIndex:0];
NSString *filePath = [documentsDir stringByAppendingPathComponent:yourFileName];
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager createFileAtPath:filePath contents:nil attributes:nil];
// Open a handle to it.
NSFileHandle* pfile = [NSFileHandle fileHandleForWritingAtPath:filePath];
[pfile writeData:[[NSString stringWithString:#"YourData"] dataUsingEncoding:NSUTF8StringEncoding]];
And to provide user facility to edit it you can use, one option I can see.
Enable "Application supports iTunes file sharing"in plist file of the application so that user would be able to see the documents through the itunes.
You need to read and check the content though to track the changes done to document.