3

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.

user1246957
  • 103
  • 2
  • 8
  • As an aside, it can be helpful to create the string separately from the write statement, that way the string can be verified in the debugger and/or `NSLog` statements. There are a lot of methods calls in the write statement. See @EricS for the answer to your question. – zaph Apr 15 '12 at 14:35

1 Answers1

6

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];
  ...
}
EricS
  • 9,650
  • 2
  • 38
  • 34
  • Any reason why you need to test paths.count > 0? – user523234 Apr 15 '12 at 14:36
  • Paranoia mostly. If you don't test it and its empty, calling [paths objectAtIndex:0] will throw an exception. While it's unlikely in this particular case for it to be empty, there are times when it happens on the Mac since users can move and rename directories. I would probably create the directory if it didn't exist using NSFileManager's createDirectoryAtPath:withIntermediateDirectories:attributes:error method. – EricS Apr 15 '12 at 14:50
  • And then how could I get this file.txt to my Computer? using email ? or there are some other better options ? – user1246957 Apr 15 '12 at 15:13
  • email is one option. iCloud or DropBox are other options. Or perhaps iOS file sharing as described at http://support.apple.com/kb/HT4094 . – EricS Apr 15 '12 at 15:17