PDA

View Full Version : writing to an xml file in the main bundle




Chirone
Jul 9, 2009, 12:00 AM
when i try to write to an xml file it doesn't seem to have changed

I put the xml file into the main bundle (in the resources folder) and i can read from it fine.

i just can't write to it

this is what i'm doing to write to it... i know there's something i'm doing wrong, i just can't see what it is...


NSBundle *bundle = [NSBundle mainBundle];
NSString* fileName = [bundle pathForResource: @"theXMLFile" ofType: @"xml"];

if(!fileName) return;

NSURL* url = [NSURL fileURLWithPath: fileName];
NSXMLDocument* document = [[NSXMLDocument alloc] initWithContentsOfURL: url options: NSXMLDocumentTidyXML error: nil];

NSXMLElement* root = [document rootElement];

NSXMLElement* node = (NSXMLElement*)[NSXMLNode elementWithName: @"aTestElement"];
[root addChild: node];

NSData *xmlData = [document XMLDataWithOptions: NSXMLNodePrettyPrint];
if(![xmlData writeToFile: fileName atomically: YES]) {
NSBeep();
NSRunAlertPanel(@"Save Report Builder Settings Error", @"Could not save the Report Builder Settings", @"OK", NULL, NULL);

}
NSRunAlertPanel(@"Adding New Preset", @"New Preset Added", @"OK", NULL, NULL);

in debug mode i see that root is being added to correctly, and the new element is being added to it...
it's just not printing out to it...



robbieduncan
Jul 9, 2009, 03:50 AM
Don't ever write into the main bundle. You cannot guarantee that it's writable and this is directly against all Apple direction. For example the main bundle could be on a network drive, or the user may not be an Administrator. Use the official locations, specifically in this case the Application Support folder.

Chirone
Jul 9, 2009, 03:58 AM
oh right...

well it seemed to me that it was doing something... it wasn't writing to the file exactly, the file in xcode never changed, but it did read from the file with the changes made....

if i need to allow users to define their own settings for something then where best is it to store the info?

robbieduncan
Jul 9, 2009, 04:09 AM
oh right...

well it seemed to me that it was doing something... it wasn't writing to the file exactly, the file in xcode never changed, but it did read from the file with the changes made....

if i need to allow users to define their own settings for something then where best is it to store the info?

NSUserDefaults for user settings, Application Support for everything else. This is covered in the documentation (somewhere in the guides about building quality OSX software). Updating a file in the bundle was never going to change the file in XCode anyway: the file in the bundle is copied from the file in XCode every build.

You can find the correct path for the Application Support directory and similar using NSSearchPathForDirectoriesInDomains (http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Functions/Reference/reference.html#//apple_ref/c/func/NSSearchPathForDirectoriesInDomains).

Chirone
Jul 9, 2009, 06:46 AM
can i write to a file inside the .app folder? or is that the main bundle..?

sorry for the noob question and if you've already answered it..

robbieduncan
Jul 9, 2009, 06:55 AM
can i write to a file inside the .app folder? or is that the main bundle..?

sorry for the noob question and if you've already answered it..

Whilst you might be able to you shouldn't for the reasons I mentioned above. In addition if you have use the code signing facilities to provide your users with reassurance that your app has not been tampered with then doing so will break that. Basically don't.

Edit to add: Apple's Documentation (http://developer.apple.com/documentation/MacOSX/Conceptual/BPFileSystem/Articles/WhereToPutFiles.html)

Chirone
Jul 9, 2009, 10:43 PM
do you know how to create a folder in the application support if it doesn't exist already?

google isn't telling me..

nevermind, found NSFileManager :)