Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Chirone

macrumors 6502
Original poster
Mar 2, 2009
279
0
NZ
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...

Code:
		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...
 
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.
 
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?
 
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.
 
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..
 
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
 
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 :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.