1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

writing to an xml file in the main bundle

Discussion in 'Mac Programming' started by Chirone, Jul 8, 2009.

  1. macrumors 6502

    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]) {
    			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...
  2. Moderator


    Staff Member

    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.
  3. macrumors 6502

    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?
  4. Moderator


    Staff Member

    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.
  5. macrumors 6502

    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..
  6. Moderator


    Staff Member

    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
  7. macrumors 6502

    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 :)

Share This Page