Question

found a few things on this issue but so far nothing has worked.

I'm trying to write to the end of a file using the following code:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Filename" ofType:@"txt"];
NSString *textToWrite = @"Test";
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];
[fileHandle seekToEndOfFile];
[fileHandle writeData:[textToWrite dataUsingEncoding:NSUTF8StringEncoding]];
[fileHandle closeFile];

When I step through the code filePath gets set to "/var/mobile/Applications/1669D96A-1ABA-4A4F-D083-A90EB728691B/MyApp.app/Filename.txt".

However the NSFileHandle fileHandleForWritingAtPath returns nil. The code doesn't raise an exception/warning and continues as though fine. When I read the file into a string the new text is not there, only the stuff that is initially in the file (typed in Xcode). I use the exact same code to get the path for reading the file, and that works!

Was it helpful?

Solution

The app bundle is read-only. You can't write to any path in the bundle. That is why you get a nil file handle.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top