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"];
Related
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.
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];
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
Can someone explain and give example code to do the following steps in my iPad app:
do some things in my app, which generates some data (as a string)
write that data to a text file
be able to plug in my iPad to my computer and grab those text files off
How can I do this?
For writing string in a file use this
NSString *str = #"your string";
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* filePath = [documentsDirectory stringByAppendingPathComponent:#"data.txt"];
NSError *error;
[str writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
Then set Application supports iTunes file sharing key to YES in your plist file.
When you connect your device with iTunes from there you can save your data.txt
Here is the video how to get files from iTunes
Edited as requested.
NSUserDefaults *deflt = [NSUserDefaults standardUserDefaults];
//this number file is saved, I'm not saving the file name as data0.txt.
//The first file to be saved is data1.txt
int num = [deflt integerForKey:#"fileNameNum"];
num++;
NSString *fileName = [NSString stringWithFormat:#"data%d.txt",num];;
NSString *str = #"your string";
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* filePath = [documentsDirectory stringByAppendingPathComponent:fileName];
NSError *error = nil;
[str writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
if (error == nil) {
//Save the number of the file that you have save in doc directory
[deflt setInteger:num forKey:#"fileNameNum"];
}
i have a local html file that i can display on UIWebview now is there a way like i can pass my string value to that html page and display the content???
this is how i am loading html
NSString *urlAddress = [[NSBundle mainBundle] pathForResource:#"index4" ofType:#"html"];
NSURL *instructionsURLd = [[NSURL alloc] initFileURLWithPath:urlAddress];
[webView loadRequest:[NSURLRequest requestWithURL:instructionsURLd]];
now index4.html content is
<html>
<body>
testing hhhhhhhhhhhhhhhhhhhhhhhhhhhhh
index1
<TD><INPUT TYPE=BUTTON OnClick="Circle_calc(this.form);" VALUE="calculate"></TD>
</body>
</html>
//
now lets say i have NSString s=#"hello"
now can i add this value to the < body > tag of html and show in UIwebview??
You cannot store mutable (changing) objects in your application bundle. However, you can put them there before building you app, open them when the app is executing, and write them to disk, and modify them whenever you want, after that.
Your code
NSString *s;
s = #"hello";
NSString *urlAddress = [[NSBundle mainBundle] pathForResource:#"index4" ofType:#"html"];
NSURL *instructionsURLd = [[NSURL alloc] initFileURLWithPath:urlAddress];
NSError *error;
When your starts for the first time only Grab the resource and store it into a string
NSString *myHTML = [NSString stringWithContentsOfURL:instructionsURLd encoding:NSASCIIEncoding error:&error];
Immediately write the data to disk
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSData *myData = [NSData dataWithBytes:[myHTML UTF8String] length:[myHTML lengthOfBytesUsingEncoding:NSUTF8StringEncoding]];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:#"index4.html"];
[myData writeToFile:appFile atomically:YES];
Then, load it when you need it
NSArray *paths2 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory2 = [paths2 objectAtIndex:0];
NSString *appFile2 = [documentsDirectory2 stringByAppendingPathComponent:#"index4.html"];
NSData *mySavedData = [[[NSData alloc] initWithContentsOfFile:appFile2] autorelease];
NSString *myNewHTML = [[NSString alloc] initWithData:mySavedData encoding:NSUTF8StringEncoding];
Insert s into the HTML document
myNewHTML = [myHTML stringByReplacingOccurrencesOfString:#"<body>" withString:[NSString stringWithFormat:#"<body> %#",s]];
Write everything back to the file
NSArray *paths3 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory3 = [paths objectAtIndex:0];
NSMutableData *myData3 = [NSData dataWithBytes:[myNewHTML UTF8String] length:[myNewHTML lengthOfBytesUsingEncoding:NSUTF8StringEncoding]];
NSString *appFile3 = [documentsDirectory3 stringByAppendingPathComponent:#"index4.html"];
NSData *myNewData = [NSData dataWithBytes:[myNewHTML UTF8String] length:[myNewHTML lengthOfBytesUsingEncoding:NSUTF8StringEncoding]];
[myNewData writeToFile:appFile3 atomically:YES];
Want to improve this post? Provide detailed answers to this question, including citations and an explanation of why your answer is correct. Answers without enough detail may be edited or deleted.
You can simply do this by creating javascript method in HTML and then call it via IOS if you want to pass that string to html from IOS