It works for me in cocoa.
NSString * xmlData = @"This is some random string";
NSString * xmlString = [NSString stringWithString:xmlData];
NSError * error = nil;
if (![xmlString writeToFile:@"data.txt"
atomically:NO
encoding:NSASCIIStringEncoding
error:&error])
{
NSLog(@"writeToFile failed: %@", error);
}
I would check:
- How do you get xmlData? Is it a NSString?
- Do you specify file path within your app bundle? You will not be able to write outside your application directory apart from Documents (?) I think.
This is how you would specify file in Documents directory:
// Documents directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
// <Application Home>/Documents/foo.plist
NSString *fooPath = [documentsPath stringByAppendingPathComponent:@“foo.plist”];