I have seen a lot of threads on downloading images to the apps directory. What i want to know is how to download a text file and then find it. Is this possible?
I am sorry but I still cannot get it. I looked at your pretty cool google search and only found definitions. I really just want some sample code. if you have worked with this before that would be great. I am just so new at this. I read everything on google from apple and other answers. But still have not found my answer. THank you for the quick responses.
Yes. Look at my 2ndpost. Use NSFileManager to save the txt file to your application documents directory.
Then always, before downloading the .txt file, check if the file exists in your documents directory first. If it does, then you dont need to downlaod the text file. Otherwise, you do.
I've tried quite a few times but that never works. Do have a code snippet that I can compare my code to. If not when I get home I'll post the code that I tried using.
When you get home, just post your code. I think it's better to show you what you're doing wrong in your own code than just give you the right code that works.
Here it is. I don't know if this is correct. I got it from somebody.
Code:
NSError *error = nil;
NSString *str;
// Search if the file exists already in the documents directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
filePath = [documentsDirectory stringByAppendingPathComponent:@"myFile.txt"];
// If file doesn't exists in the docDirectory or it's empty then I take it from the MainBundle
if (filePath) {
fileContent = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
if (!fileContent) {
filePath = [[NSBundle mainBundle] pathForResource:@"myFile" ofType:@"txt"];
}
}else {
filePath = [[NSBundle mainBundle] pathForResource:@"myFile" ofType:@"txt"];
}
// Once the file is taken (no matter if it comes from the docDirectory or the MainBundle) it's time to start working with it:
if (filePath) {
fileContent = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
if (fileContent) {
** * * * * * * */* // You can use this if you want to read from the file!
NSArray* arr = [fileContent componentsSeparatedByString:@"\n"];
NSEnumerator* nse = [arr objectEnumerator];
str = [nse nextObject];
** * * * * * * **/
** * * * * * * *// Prepare the path to write to the file...
filePath = [documentsDirectory stringByAppendingString:@"/"];
filePath = [filePath stringByAppendingString:@"myFile.txt"];
** * * * * * * *NSString *string = @"Hello, world!";
// Write string to file
if ([string writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error: nil]) {
//NSLog(@"File writed correctly!");
}else {
//NSLog(@"ERROR: Unable to write to file!");
}
}
}
Here it is. I don't know if this is correct. I got it from somebody.
If you ever have any hopes of producing any kind of applications, dont just bolt together snippets of code from all over the place. Or at least if you do, you should know what the code actually does, and be able to tell if it is working correctly or not.
I read the entire forum which gave me a great idea of what is happening. The problem is that there are no tutorials on this so I can't really learn anything quickly. Do you know anything that would help.
First I use the code that blue monkey gave mr and I saved the contents of the URL to a string. Then I wrote to a text file in the documents directory from this tutorial. The link to the video is below.
I have one last question. When I implement this the UIFreezes and I am unable to show that the application is doing something. Is it possible to allow it to do it in the background or at least show a UIActivityIndicatorView animating?
I used the initWithContentsOfURL:blah and then saved it to the documents directory using the tutorial posted above.
I have one last question. When I implement this the UIFreezes and I am unable to show that the application is doing something. Is it possible to allow it to do it in the background or at least show a UIActivityIndicatorView animating?
I used the initWithContentsOfURL:blah and then saved it to the documents directory using the tutorial posted above.
Other than NSURLConnectioon, you can just use the performSelectorInBackground method and call your initWithContents of URL there. You can add an activity indicator as well