Saving dropbox file in path - objective-c

This gives me...
NSString *filename2 = [NSString stringWithFormat:#"/newFile.json"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#", filename2]];
[self.restClient loadFile:filename2 intoPath:getImagePath];
NSString *fileContent = [[NSString alloc] initWithContentsOfFile:getImagePath];
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *data = (NSDictionary *) [parser objectWithString:fileContent error:nil];
...this error:
File loaded into path: (null)
...but why?? this code was under view did load. the file is in dropbox, so what is the problem

Related

How to Save data into Project Folder?

I download a XML file from web and save it in a folder.With these Codes
NSURL *goo = [[NSURL alloc]initWithString:#"......xml"];
NSData *data = [[NSData alloc] initWithContentsOfURL:goo];
NSString *xml = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSString *documentsDirectory = #"/Users/MacBook/Desktop/Bebegim_Icin_Yarisiyorum/Milyoner/Class";
NSString *xmlFilePath = [documentsDirectory stringByAppendingPathComponent:#"qpack1.xml"];
[xml writeToFile:xmlFilePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
Class Folder is in myProject Folders.
But Apple does not have this path.I need to say save the
xml file in Class folder of My project Folders.
/Users/MacBook/Desktop/Bebegim_Icin_Yarisiyorum/Milyoner/Class
Can anyone help me,I should say ../Class or .Class or ./Class?
What should I say when I am creating documentDirectory.
Try:
NSString *documentsDirectory = [NSHomeDirectory()stringByAppendingPathComponent:#"Documents/qpack2.xml"];
Or:
NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)
NSString *docsPAth = [pathArray objectAtIndex:0];
NSString *documentsDirectory = [documentsDirectory stringByAppendingPathComponent:#"qpack2.xml"];

Objective-C save and load files

Hello I am trying to load data from a file
This function save the data to a file in documents folder
- (IBAction)saveUser:(id)sender{
NSString *name = [nameField stringValue];
NSString *weight = [weightField stringValue];
NSDate *date = [datePick dateValue];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setValue:[nameField stringValue] forKey:#"name"];
[dict setValue:[weightField stringValue] forKey:#"weight"];
[dict setValue:date forKey:#"date"];
[dict writeToFile:name atomically:YES];
}
However when I try to load the file and get the data from it I cant, could someone tell me how this is done done.
Edit to show loadFunction
-(IBAction)loadUser:(id)sender{
NSString *weight = [weightField stringValue];
NSDate *date = [datePick dateValue];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *loadPath = [documentsDirectory stringByAppendingPathComponent:name];
NSMutableDictionary *savedData = [[NSMutableDictionary alloc] initWithContentsOfFile: name];
NSLog(#"%#", weight);
}
To fix your answer. here is a code that works, you pretty much did not give a filename.
NSString *filename = [documentsDirectory stringByAppendingPathComponent:#"file.txt"];
And in your line where you write the file :
[dict writeToFile:name atomically:YES];
you must change name to filename, like this
[dict writeToFile:filename atomically:YES];
And in your load data method :
NSString *filename = [documentsDirectory stringByAppendingPathComponent:#"file.txt"];
NSMutableDictionary *savedData = [[NSMutableDictionary alloc] initWithContentsOfFile:filename];
NSLog(#"The weight is : %#", [savedData valueForKey:#"weight"]);
The following seems to do what you intend (minus the UI element dependencies in your code):
#import <Foundation/Foundation.h>
static NSString *pathToDocuments(void) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return [paths objectAtIndex:0];
}
int main(int argc, char *argv[]) {
NSAutoreleasePool *p = [[NSAutoreleasePool alloc] init];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setValue:#"SomeName" forKey:#"name"];
[dict setValue:#"SomeWeight" forKey:#"weight"];
[dict setValue:[NSDate date] forKey:#"date"];
NSString *filePath = [pathToDocuments() stringByAppendingPathComponent:[dict objectForKey:#"name"]];
[dict writeToFile:filePath atomically:YES];
[dict release]; dict = nil;
NSLog(#"%s - Confirming dict is nil: %#",__FUNCTION__,dict);
dict = [[NSDictionary dictionaryWithContentsOfFile:filePath] mutableCopy];
NSLog(#"%s - weight = %#",__FUNCTION__,[dict objectForKey:#"weight"] );
[p release];
}
This prints the following to the console:
2012-10-19 06:40:38.691 Untitled[10633:707] main - Confirming dict is nil: (null)
2012-10-19 06:40:38.693 Untitled[10633:707] main - weight = SomeWeight
EDIT:
That said, I think the problem may be the file path in loadUser...

Saving NSMutableDictionary to plist iOS

This has become very frustrating over the past day-and-a-bit-too-much: why doesn't this work?
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.plist",kAllScoresUnorderedArray]];
NSMutableDictionary *test = [[NSMutableDictionary alloc] init];
[test setObject:#"bob" forKey:#"tmo"];
[test writeToFile:path atomically: YES];
I have used it before, and it worked perfectly: is there something I am forgetting to add in my info.plist? That is the only thing I can think of.
Thank you for helping!
EDIT:
Maybe it has something to do with Xcode 4.5 and iOS6? It worked in 4.3/iOS5
Have you added the .plist file to your project? If so, try this:
NSError *error;
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.plist",kAllScoresUnorderedArray]];
if (![fileManager fileExistsAtPath: path])
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:kAllScoresUnorderedArray ofType:#"plist"];
[fileManager copyItemAtPath:bundle toPath: path error:&error];
}
NSMutableDictionary *test = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
[test setObject:#"bob" forKey:#"tmo"];
[test writeToFile:path atomically: YES];
If not, create the property list in your project.

Reading a file from documents directory Objective-c

I write a file to the documents directory of an app:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// the path to write file
NSString *file = [documentsDirectory stringByAppendingPathComponent:#"data.json"];
[responseString writeToFile:file atomically:YES];
But how can I access the file?
My code looks like this:
NSString *filePath = [[NSFileManager defaultManager] pathForRessorce: #"data" ofType:#"JSON"];
NSString *fileContent = [[NSString alloc] initWithContentsOfFile:filePath];
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *data = (NSDictionary *) [parser objectWithString:fileContent error:nil];
The file path is not populated...can somebody give me a hint?
Thanks!
You are reading from resource folder, not from document directory. You need to populate filePath in the same way that you have used during writing. That is:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"data.json"];
NSString *fileContent = [[NSString alloc] initWithContentsOfFile:filePath];
"initwithcontentsoffile is deprecated" warning
So Instead of:
NSString *fileContent = [[NSString alloc] initWithContentsOfFile:filePath];
use:
NSString *userInfoFileContent = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];

I have successfully saved my plist to the documents directory! Now I can't load it back

This code works beautifully to save my plist to the documents directory.
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = self.addButtonItem;
NSString *path = [[NSBundle mainBundle] pathForResource:#"TitleArray" ofType:#"plist"];
NSMutableArray *tmpArray = [[NSMutableArray alloc]initWithContentsOfFile:path];
self.titles = tmpArray;
[tmpArray release];
//TESTING NEW CODE FOR SAVING TO DOC DIR
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *docDirPath = [documentsDirectory stringByAppendingPathComponent:#"TitleArray.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath: docDirPath])
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:#"TitleArray" ofType:#"plist"];
[fileManager copyItemAtPath:bundle toPath:docDirPath error:&error];
NSLog(#"plist is copied to Directory");
}
I have not been able to figure out how to load the plist back to the app!
Can anyone help? Thanks.
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"TitleArray.plist"];
NSArray * myList = [NSArray arrayWithContentsOfFile:path];
NSMutableArray *tmpArray = [myList mutableCopy];
self.titles = tmpArray;
[tmpArray release];
didn't work?
If you are asking how to overwrite a plist included in your app's main bundle, then you should know that it cannot be done. Take a look at this question for more details: Can you update a file in the application bundle?
NSError *error; NSArray *paths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *docDirPath = [documentsDirectory stringByAppendingPathComponent:#"TitleArray.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath: docDirPath])
{
docDirPath = [[NSBundle mainBundle] pathForResource:#"TitleArray" ofType:#"plist"];
}
NSMutableArray *tmpArray = [[[NSMutableArray alloc] initWithContentsOfFile: docDirPath] mutableCopy];