22

I'm trying to create a text file with the contents of a string to my desktop. I'm not sure if I'm doing it right, I don't get errors but it doesn't work either...

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, NSUserDomainMask, YES);
NSString *desktopDirectory=[paths objectAtIndex:0];
NSString *filename = [desktopDirectory stringByAppendingString: @"file.txt"];
[myString writeToFile:filename atomically:YES encoding: NSUTF8StringEncoding error: NULL];
Joshua Nozzi
  • 60,946
  • 14
  • 140
  • 135
ACV
  • 221
  • 1
  • 2
  • 3

3 Answers3

53
//Method writes a string to a text file
-(void) writeToTextFile{
    //get the documents directory:
    NSArray *paths = NSSearchPathForDirectoriesInDomains
        (NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    //make a file name to write the data to using the documents directory:
    NSString *fileName = [NSString stringWithFormat:@"%@/textfile.txt", 
                                                  documentsDirectory];
    //create content - four lines of text
    NSString *content = @"One\nTwo\nThree\nFour\nFive";
    //save content to the documents directory
    [content writeToFile:fileName 
                     atomically:NO 
                           encoding:NSStringEncodingConversionAllowLossy 
                                  error:nil];

}
bren
  • 4,176
  • 3
  • 28
  • 43
megha
  • 905
  • 7
  • 17
  • Thanks, mate! Simple and easy to use. – Felipe Mar 17 '17 at 21:13
  • Note that NSStringEncodingConversionAllowLossy shouldn't be used there, it's not a string encoding type but it's an encoding option that happens to have the same value as NSASCIIStringEncoding (1) – Maxweel Apr 14 '17 at 21:12
  • I might have gone with `NSString *fileName = [documentsDirectory stringByAppendingPathComponent: @"textfile.txt"];`. – Mike May 31 '20 at 20:46
14

You don't know if you're getting any errors because you're ignoring the returned YES/NO value of the -writeToFile:... method, and giving it no error pointer into which to record any possible failure. If the method returns NO, you'd check (and handle or present) the error to see what went wrong.

At a guess, the failure is due to the path you constructed. Try -stringByAppendingPathComponent: instead of -stringByAppendingString: ... this and its related methods properly handle paths.

The file probably is actually being created (ie, you might not be getting any errors after all). My guess is the file is created somewhere like "~/Desktopfile.txt" since your use of -stringByAppendingString: doesn't consider the string as slash-separated path. Check your home folder - I'll bet the file's there.

Joshua Nozzi
  • 60,946
  • 14
  • 140
  • 135
-3

the problem is that the desktop directory string ends in nothing (no /). Check this out (on an iPhone) by using UIAlertview.

Eman yalpsid
  • 511
  • 1
  • 5
  • 8
  • 1
    What? 1.) You don't need to use a `/` if you do it the way he did. 2.) Where do you see a UIAlertView? I don't. 3.) this question was asked almost 2 years ago. – Matt S. Aug 14 '11 at 17:43
  • ..Also why would we check out something to do with a "Desktop" on iOS? iOS has no such thing. – Max von Hippel Aug 26 '15 at 00:49