//Method writes a string to a text file
-(void) writeToTextFile{
//get the documents directory:
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//make a file name to write the data to using the documents directory:
NSString *fileName = [NSString stringWithFormat:@"%@/textfile.txt", documentsDirectory];
//create content - four lines of text
NSString *content = @"One\nTwo\nThree\nFour\nFive";
//save content to the documents directory
[content writeToFile:fileName atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil];
}
//Method retrieves content from documents directory and
//displays it in an alert
-(void) displayContent{
//get the documents directory:
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//make a file name to write the data to using the documents directory:
NSString *fileName = [NSString stringWithFormat:@"%@/textfile.txt", documentsDirectory];
NSString *content = [[NSString alloc] initWithContentsOfFile:fileName usedEncoding:nil error:nil];
//use simple alert from my library (see previous post for details)
[ASFunctions alert:content];
[content release];
}
Wednesday, December 7, 2011
Write/Retrieve file to/form document directory
Delete database file from the Apps document directory
To delete a .db file in the Apps Document Directory.
The below code shows how to select that exact .db file and path, then remove it from the direcotory.
// create the path to access the document directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
// create the path to access the 1st level under doucment directory
NSString *secondaryDirectoryPath = [documentsDirectoryPath
stringByAppendingPathComponent:@"secondary"];
// create the path to access database file
NSString *databaseFile = [secondaryDirectoryPath stringByAppendingPathComponent:@"database.db"];
// init fileManager and remove the database file
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:databaseFile error:NULL];
The below code shows how to select that exact .db file and path, then remove it from the direcotory.
// create the path to access the document directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
// create the path to access the 1st level under doucment directory
NSString *secondaryDirectoryPath = [documentsDirectoryPath
stringByAppendingPathComponent:@"secondary"];
// create the path to access database file
NSString *databaseFile = [secondaryDirectoryPath stringByAppendingPathComponent:@"database.db"];
// init fileManager and remove the database file
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:databaseFile error:NULL];
Tuesday, December 6, 2011
NSUserDefault
Q: Added NSUserDefault data retrieval to the app. For testing I would like to reset all the data I added to the defaults database, so that everything is in the state when the user launches the app the first time.
A: The easiest way is to remove the app from the simulator-- just like you'd remove it from a real phone, by tapping (clicking) and holding until the icons start vibrating. That removes all app data, and the next time you install from Xcode it's like the first time.
If you have other app data you need to keep, you have a couple of options.
One way would be to have some debug code that calls removeObjectForKey: on each of your defaults keys.
The other is to find the directory where the simulator copy is installed, and remove the file containing the preferences. Use this to find the app:
ls -ld ~/Library/Application\ Support/iPhone\ Simulator/User/Applications/*/*.app
The full path to your app will contain directory whose name is a UUID. In that directory, look in Library/Preferences for the preferences file. Remove that, and user preferences are gone.
Subscribe to:
Posts (Atom)