Home Directory
1 |
DLOG(@"HOME > %@", NSHomeDirectory()); |
Naturally, you’d have the documents folder in your home directory as well:
1 |
NSString * homeDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; |
which will give you something like this:
homeDir is: /var/mobile/Applications/DF2ACE23-C3D0-444B-9D25-7E56F102595A/Documents
This is where you save your files in your sandbox.
Writing text to a txt file
First, you have to get the name of your path + file. Something like this:
/var/mobile/Applications/DF2ACE23-C3D0-444B-9D25-7E56F102595A/Documents/test.txt
We do this by appending our file name to the home directory. In our case, let’s say self.filename is test.txt:
1 |
filepath = [self.homeDir stringByAppendingPathComponent:self.filename]; |
Then you use that path string and use method writeToFile: to write contents to that file:
1 |
BOOL ok = [textToWrite writeToFile:filepath atomically:YES encoding:NSUnicodeStringEncoding error:&err]; |
full method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
-(void)WriteToStringFile:(NSMutableString *)textToWrite { filepath = [[NSString alloc] init]; NSError *err; filepath = [self.GetDocumentDirectory stringByAppendingPathComponent:self.filename]; DLOG(@"file path is: %@", filepath); BOOL ok = [textToWrite writeToFile:filepath atomically:YES encoding:NSUnicodeStringEncoding error:&err]; if (!ok) { NSLog(@"Error writing file at %@n%@", filepath, [err localizedFailureReason]); } [filepath release]; } |
Reading text from file in sandbox
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
-(NSString *) readFromFile { filepath = [[NSString alloc] init]; NSError *error; NSString * title = @"uh oh"; filepath = [self.GetDocumentDirectory stringByAppendingPathComponent:self.filename]; DLOG(@"file path is: %@", filepath); NSString * txtInFile = [[NSString alloc] initWithContentsOfFile:filepath encoding:NSUnicodeStringEncoding error:&error]; if(!txtInFile) { UIAlertView *tellErr = [[UIAlertView alloc] initWithTitle:title message:@"Unable to get text from file." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [tellErr show]; } return txtInFile; } |
Writing image to your sandbox
We first create an image. Make sure the image resource exists.
Then we give it a string name and then make sure homeDir is same as we’ve displayed above. Plug in and use like so:
1 2 |
UIImage * myImage = [UIImage imageNamed:@"refresh.png"]; [myFile saveImage:myImage withFileName:@"refresh" ofType:@"png" inDirectory: myFile.homeDir]; |
where myFile.homeDir is: /var/mobile/Applications/DF2ACE23-C3D0-444B-9D25-7E56F102595A/Documents
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
-(void) saveImage:(UIImage *)image withFileName:(NSString *)imageName ofType:(NSString *)extension inDirectory:(NSString *)directoryPath { NSString * fullImageName = [NSString stringWithFormat:@"%@.%@", imageName, @"png"]; NSString * fullDirectoryPath = [directoryPath stringByAppendingPathComponent:fullImageName]; if ([[extension lowercaseString] isEqualToString:@"png"]) { if([UIImagePNGRepresentation(image) writeToFile:fullDirectoryPath options:NSAtomicWrite error:nil]) { DLOG(@"png written successfully"); } } /* else if ([[extension lowercaseString] isEqualToString:@"jpg"] || [[extension lowercaseString] isEqualToString:@"jpeg"]) { if([UIImageJPEGRepresentation(image, 1.0) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"jpg"]] options:NSAtomicWrite error:nil]) { DLOG(@"jpeg written successfully"); } } */ else { DLOG(@"Image Save FailednExtension: (%@) is not recognized, use (PNG/JPG)", extension); } } |
Reading images from your sandbox
Used like this:
1 |
UIImage * tmp = [myFile loadImage:@"refresh.png"]; |
Implementation:
1 2 3 4 5 6 |
-(UIImage*)loadImage:(NSString*)imageName { NSString * pathComponent = [NSString stringWithFormat:@"Documents/%@", imageName]; NSString *imagePath = [NSHomeDirectory() stringByAppendingPathComponent:pathComponent]; return [UIImage imageWithContentsOfFile:imagePath]; } |