writeToFile path? - iphone

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.

Related

I can't find/see my .plist folder that were saved using the Xcode simulator?

I'm trying to screw around with the encoding files (doing a tutorial). Anyways, I trying to save a file called, "Checklists.plist" and can't find it in my app location:
USERINFOSTUFF/Library/Application Support/iPhone Simulator/7.0.3/Applications/APP NUMBER/Library/Documentation/Checklists.plist
ALSO, the documentation folder in the library is doesn't exist according to terminal when I try to unhide it even though Xcode says that it does exist...
The bold is just to get rid of the jargon. Here's the code:
- (NSString *)documentsDirectory
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return documentsDirectory;
}
-(NSString *)dataFilePath
{
return [[self documentsDirectory] stringByAppendingPathComponent:#"Checklists.plist"];
}
-(void)saveChecklistItems
{
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiever = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiever encodeObject:items forKey:#"ChecklistItems"];
[archiever finishEncoding];
[data writeToFile:[self dataFilePath] atomically:YES];
}
I got the paths using:
NSLog(#"Document is at: %#", [self documentsDirectory]);
NSLog(#"Data file path is at: %#", [self dataFilePath]);
Change:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
to this:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

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];

Where To Add File Save Method?

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];
}

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.