My app downloads text files from a server in order to determine the content of the files. There's one problem, I am changing the files on the server, editing their content, but when the app downloads them the files have no changed. Loading the files up in a browser shows that the changes did take effect. Somehow the app is accessing an older version of the file that shouldn't exist. Here's my code:
-(NSArray *)writeFile:(NSString *)section :(NSString *)item{
NSString *fileName = [NSString stringWithFormat:#"http://myurl.com/%#/%#.txt", section, item];
fileName = [fileName stringByReplacingOccurrencesOfString:#" "
withString:#"%20"];
NSString *data = [[NSString alloc] initWithContentsOfURL: [NSURL URLWithString: fileName] encoding:NSUTF8StringEncoding error:nil];
NSLog(#"Read from file: %#", fileName);
NSLog(#"Content: %#", data);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.txt", item]];
[data writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
NSString* content = [NSString stringWithContentsOfFile:path
encoding:NSUTF8StringEncoding
error:NULL];
NSArray * arrayData = [content componentsSeparatedByCharactersInSet:
[NSCharacterSet characterSetWithCharactersInString:#"\n\n"]
];
return arrayData;
}
There's one file that is only a single character, a number, and THAT file downloads correctly each time. The rest of the files are larger with multiple lines. When I move the files to a different server they will download the correct version of the files, but if I make changes again and try to redownload from the new source, the changes are not reflected in the download (but still on the server).
I am so confused, I can't see any reason that it would do this, unless it's saving some form of a cookie. Ideas?
It was phone's fault. I was using my Note 3 as a wireless hotspot because the normal wifi connection's range is 20 feet away from where I sit. Once I hooked up to a traditional wifi, it worked just fine.
Related
I want to create a .json file and .text file public so that it can be read by NSItemProvider. I want to create file programmatically.
Can you refer this link, that will be helpful to play around file creation, deletion etc...
http://www.ios-developer.net/iphone-ipad-programmer/development/file-saving-and-loading/using-the-document-directory-to-store-files
Use the following code to write/create a .txt file in your app's Documents directory :
NSError *error;
NSString *stringToWrite = #"hello>!!new file created..";
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:#"myfile.txt"];
[stringToWrite writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
To read/fetch the text file:
NSString *str = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
NSLog(#"%#", str);
Here is my code:
-(NSArray *)getSpecialArray:(NSString *)day{
NSString *stringURL = [NSString stringWithFormat:#"%#/%#%#", #"http://www.myDomain.com/", day, #".txt"];;
stringURL = [stringURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
NSString *filePath;
if ( urlData )
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
filePath = [NSString stringWithFormat:#"%#/Area/%#%#", documentsDirectory, day, #".txt"];
[urlData writeToFile:filePath atomically:YES];
}
NSString *content = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];
if(content == nil){
content = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
}
content = [content stringByReplacingOccurrencesOfString:#"Main Menu\n" withString:#""];
splitData = [content componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"\n\n"]];
return splitData;
}
What seems to be happening is that the write to file section is not working. Content seems to always == nil on that line. It also could be that it is writing just fine and it's not reading the file well for whatever reason, though I'm leaning towards the former option. Anyone see any problems with this code? Assume this is the first time running this code and the folder and file being written to doesn't exist. This problem seemed to occur right after switching to Mountain Lion and upgrading Xcode.
The problem is in creating the URL.
NSString *stringURL = [NSString stringWithFormat:#"%#/%#%#", #"http://www.myDomain.com/", day, #".txt"];
You are appending "/" in formatter as well as with URL value.
So your final URL will become http://www.myDomain.com//filename.txt. This is a invalid URL.
Correct it by removing "/" from either of place.
NSString *stringURL = [NSString stringWithFormat:#"%#%#%#", #"http://www.myDomain.com/", day, #".txt"];
At first, check if your file is actually created on filesystem and/or check returning value of [urlData writeToFile:filePath atomically:YES]; call.
If file exists and method returns YES, then the problem is in the reading data. Pass NSError** to stringWithContentsOfFile: encoding: error: and check it. Also, don't use NULL for objects, instead use nil.
So in my app I have a bunch of data that I'd like to write to a log file, and then display it within a UITextView when I click a button. I know how to toggle the UITextView, but I have no idea how to create and update a log file (in the local filesystem). Thanks for any help.
The basic idea is that you create the file, and append to it every time you log a new line. You can do it quite easily like this:
Writing to the file:
NSString *content = #"This is my log";
//Get the file path
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *fileName = [documentsDirectory stringByAppendingPathComponent:#"myFileName.txt"];
//create file if it doesn't exist
if(![[NSFileManager defaultManager] fileExistsAtPath:fileName])
[[NSFileManager defaultManager] createFileAtPath:fileName contents:nil attributes:nil];
//append text to file (you'll probably want to add a newline every write)
NSFileHandle *file = [NSFileHandle fileHandleForUpdatingAtPath:fileName];
[file seekToEndOfFile];
[file writeData:[content dataUsingEncoding:NSUTF8StringEncoding]];
[file closeFile];
Reading:
//get file path
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *fileName = [documentsDirectory stringByAppendingPathComponent:#"myFileName.txt"];
//read the whole file as a single string
NSString *content = [NSString stringWithContentsOfFile:fileName encoding:NSUTF8StringEncoding error:nil];
I thought was a class out there to do this automatically as after no luck created my own.
NSLogger is a lightweight class for iOS versions 3.0 and above. It allows developers to easily log different 'events' over time which are locally stored as a .txt file.
https://github.com/northernspark/NSLogger
What is the easiest way to programatically create file.txt on your device, then in your application write down some data and then send this data to your Computer?
I guess that the last part could be done programatically same as using some program on your computer to get data from the device.
Till now am doing something like this
NSFileHandle *infile;
NSFileManager *fm;
fm=[NSFileManager defaultManager];
NSString *strings=[#"/Downloads/file.txt" stringByExpandingTildeInPath];
if (![fm fileExistsAtPath:strings]) {
[fm createFileAtPath:strings contents:nil attributes:nil];
infile=[NSFileHandle fileHandleForWritingAtPath:strings];
[infile writeData:[[NSString stringWithFormat:#"lalala japierdole\n", [NSDate date]] dataUsingEncoding:NSUTF8StringEncoding]];
}else {
infile=[NSFileHandle fileHandleForWritingAtPath:strings];
[infile seekToEndOfFile];
}
[infile writeData:[[NSString stringWithFormat:#"lulukukuioio.\n", [NSDate date]] dataUsingEncoding:NSUTF8StringEncoding]];
This is just creating the file if it is not exist and writing down some data but I have a problem with path. This code works fine when I am working with my simulator when am trying this on my device then I really don't know if I even create the file and how to receive from it later.
The easiest APIs for reading & writing raw data to files:
NSData *data = [NSData dataWithContentsOfFile:path];
[data writeToFile:path atomically:NO];
The reason you are having problems is that your path is not valid on the device. Try this:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
if ( paths.count > 0 )
{
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:#"file.txt"];
NSData *data = [NSData dataWithContentsOfFile:path];
...
}
My app writes an encrypted data of the user info/preferences into a file, and reads from that file the next time the app is opened.
Writing a file:
- (BOOL)writeFile:(NSString *)data:(NSString *)fileName {
return [data writeToFile:fileName
atomically:YES
encoding:NSUTF8StringEncoding error:nil];
}
Reading a file:
- (NSString *)readFile:(NSString *)fileName {
NSData *data = [NSData dataWithContentsOfFile:fileName];
NSString *str = [[[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding] autorelease];
return str;
}
This works fine on the emulator. The files are written and read as expected. Is there anything I have to setup for file read/write on devices?
The filename has to be in the documents directory. The simulator won't have as many restrictions on where it can write files as the device does.
Obtain the documents directory as follows:
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *fileName = [documentsDirectory stringByAppendingPathComponent:#"myfilename.extension"];
Pass this into your functions above and you should be fine.