Radu
Radu

Reputation: 3494

How do I delete the contents of a .txt file in my mainBundle

I need to delete the contents of a .txt file and I don't know how to delete the full text.

The file is in my mainBundle. I can access the file but I can't find the command to delete the text from inside it.

 NSFileManager *filemgr;        
    filemgr = [NSFileManager defaultManager];
    NSString* path = [[NSBundle mainBundle] pathForResource:@"UserData" 
                                                     ofType:@"txt"];


    NSString* content = [NSString stringWithContentsOfFile:path
                                                  encoding:NSUTF8StringEncoding
                                                     error:NULL];

Upvotes: 0

Views: 1178

Answers (5)

XCool
XCool

Reputation: 996

The proper way is to make a copy of your text file from the app bundle into the user's Documents folder and make whatever changes you want there. Preferably, you should do this the first time your user launches your app.

The contents of the app bundle is read-only and should never be modified.

Upvotes: 0

Joachim Isaksson
Joachim Isaksson

Reputation: 180987

The files in the mainBundle are not writable with a non jailbroken iPhone, so erasing the contents of the file in its current location cannot be done.

Upvotes: 1

mvds
mvds

Reputation: 47104

You shouldn't do this on iOS in production apps. The bundle is signed, so changing its contents will make the app stop working.

Upvotes: 1

Matthias Bauch
Matthias Bauch

Reputation: 90117

You can't. The apps mainBundle is readonly.

Upvotes: 5

tharani dharan
tharani dharan

Reputation: 425

try creating a new file with the same name if it is not ready- only file ..

Upvotes: 1

Related Questions