Where To Add File Save Method? - iphone

I am saving an array that is editable by the user to the plist. My question is, where in the code to I implement the code? Like one of the methods dealing with the app quitting?
This is the code
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"patientList.plist"];
[myPatients writeToFile:path atomically:YES];

I think it depends on how you've implemented your data manager. If you have a singleton to handle accessing / writing data, why not just write data to the file as your singleton array is updated? This way you can be sure data is always saved should something unexpected happen.
What Jordan suggests is definitely one route, but I would use the appWillResign vs willTerminate.

I suggest something like this:
- (void)applicationWillTerminate:(UIApplication *)application
{
[self saveCode];
}
- (void)saveCode
{
SArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"patientList.plist"];
[myPatients writeToFile:path atomically:YES];
}

Related

iOS Saving Media Picker Selections

I am trying to create an app that saves the user's media selections from a Media Picker to be reused and avoid having to re-pick the items . Is there a way to do this with the Media Picker or any example code for this?
The only way to do this is to save the media selection in the app's sandbox i.e. Documents directory for re-use. Because, you can't access the media without user intervention i.e. you've to present media picker each time whenever you wanna pick something from the gallery. You can use something like this to save an image in app's doc directory:
+(BOOL)saveImageInDocumentsDirectory:(UIImage*)image WithName:(NSString*)name {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:name];
NSData *imageData = UIImagePNGRepresentation(image);
return [imageData writeToFile:savedImagePath atomically:NO];
}
And to retrieve the images:
+(UIImage*)getImageFromDocumentsDirectoryWithName:(NSString*)name {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:name];
return [[[UIImage alloc] initWithContentsOfFile:savedImagePath] autorelease];
}
Hope it'll be helpful.

Problem with reading/writing plist

I have an MSMutableArray that contains many NSDictionaries. A dictionary is just an NSString with a key "symbol".
I am writing my array to a plist:
+ (void)writeObjectToPList:(id)myData {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"mobile-watchlist.plist"];
[myData writeToFile:path atomically:YES];
}
How can I read this back as an array?
You can read the file back like this --
NSArray *array = [NSArray arrayWithContentsOfFile:path];

writeToFile path?

I'm wondering what is the default path when saving a file using the NSArray writeToFile:atomically: method...?
[array writeToFile:#"myFile.plist" atomically:YES];
10x
It will save your file at Document directory.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = [paths objectAtIndex:0];
this is the path where your file will be saved.
I executed the following code:
if ([#"asdasdasd" writeToFile:#"foo123456.txt" atomically:YES]) {
NSLog(#"+++");
} else {
NSLog(#"---");
}
On the simulator the file foo123456.txt popped up in my root of my HD. The console printed +++, so the operation was successful.
On the device it failed, printing ---.
Use This Code.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
plistpath = [[NSString alloc] initWithString:[documentsDirectory stringByAppendingPathComponent:#"Product.plist"]];
And then
[array writeToFile:path atomically:YES];
[Whatever [Array or Dictionary] you want to Write in Plist]
If it is still not creating the plist then check whether your array or dictionary must be empty.

download file through coding

I would like to save a file in my application which I have created with code:
NSArray *arrayPathsForImg =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *pathForImg = [arrayPathsForImg objectAtIndex:0];
pathForImg = [pathForImg stringByAppendingPathComponent:#"a.txt"];
[self.importUrlData writeToFile:pathForImg atomically:YES];
Now I want to download it to my iPhone?
If you meant "read" the file:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"a.txt"];
And now use a initWithContentsOfFile: method. For example I used the code above to read from a property list and used the following code to get the data into a NSDictionary:
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:filePath];

document directory problem?

When i write Data(53MB ) to Document directory ,the data is not written when i check directly through application support path.i coded like this,
- (BOOL)writeApplicationData:(NSData *)data toFile:(NSString *)fileName
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
if (!documentsDirectory) {
NSLog(#"Documents directory not found!");
return NO;
}
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];
return ([data writeToFile:appFile atomically:YES]);
}
it works fine, but when i read the data using follwing code, the data is null, anyhelp pls?
- (NSData *)applicationDataFromFile:(NSString *)fileName {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];
NSData *myData = [[[NSData alloc] initWithContentsOfFile:appFile] autorelease];
return myData;
}
The code is working for me (on a small download).
Some thoughts, are you downloading the 53MB over the network? Perhaps you're trying to read it before it's finished? The "atomically" flag on write to file says:
If YES, the data is written to a backup file, and then—assuming no errors occur—the backup file is renamed to the name specified by path; otherwise, the data is written directly to path.
If you're downloading this Asynchronously and can't use partial results, you may have to wait for it to complete. Otherwise you can set the atomically:NO and read in the partial result.