Search code examples
iphoneemailxcode4

How do i save to a text file on my iphone app


I have an app which is almost finished and the last part is where I send the data collected via email to the respondent. I know if I use a file in my bundle I can't write to it as it will be readonly. Where else can I save my file to that I can then use the path for my email attatchment?


Solution

  • You can only write to your application's sand-boxed documents directory. Try using this:

    NSArray *documentsPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentsPaths objectAtIndex:0];
    

    documentsDir gives you the complete path as a string to your applications documents directory.

    And then you can either use - (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag or

    - (BOOL)writeToFile:(NSString *)path options:(NSDataWritingOptions)mask error:(NSError **)errorPtr
    

    to write your data to the location.