Access XML file in project bundle in Xcode 6.3.2

372 views Asked by At

I am trying to load an XML file in Xcode. The file is supposed to be loaded when the app is started, and updated with data to be saved for future use. I dragged the file in the "Supporting Files" folder in Xcode, and I can see a copy of the file in the project subdirectory: /Users/Alex/Documents/ObjectiveC projects/myProject/myProject

The code I am using to access the file is:

// Search for files in bundle

_path = [[NSBundle mainBundle] pathForResource:@"profile" ofType:@"xml"];

NSLog(@"myFile path: %@", _path);

However, when I run the app the _path is set to some other directory:

myFile path: /Users/Alex/Library/Developer/CoreSimulator/Devices
/8F2AD2A7-4875-47C9-9AB8-852339AF31A0/data/Containers/Bundle/Application
/7D9A4A78-A7CB-40E5-8CC7-6B8EE2143D2E/myProject.app/profile.xml

Also, it looks like I cannot write to this file. The content is the same if I restart the app. This is happening regardless if I run the app in the Simulator or the device. It almost looks like I am accessing some version of the file that was cashed in the bundle when I ran the up for the first time.

1

There are 1 answers

0
Bohm On BEST ANSWER

It turns out that the files in the bundle are read only. This was discussed in other posts: File write with [NSBundle mainBundle] fails

I obtained what I need by copying the file to a new file when the app is initialized the first time it runs, and then use the new file as read/write file.