views:

3189

answers:

2

I have an iPhone application in which a number of domain objects are populated with user-entered data. In order to restore state after being interrupted, these objects implement the NSCoding protocol and are written to disk (the Documents directory) in the applicationWillTerminate message. Then, when the application is launched again, the bytes of data are loaded up and the domain objects are repopulated. Code to get the documents directory is as follows:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

This worked great in the simulator but once I deployed the app to the iPhone it stopped working. The reason why is iPhone error code 513 - which apparently means "permission denied." There is a note in the iPhone dev docs that explain a little bit more -

On the device, the returned path (documentsDirectory) is similar to the following: /var/mobile/Applications/30B51836-D2DD-43AA-BCB4-9D4DADFED6A2/Documents

However, on the Simulator, the returned path takes the following form:

/Volumes/Stuff/Users/johnDoe/Library/Application Support/iPhone Simulator/User/Applications/118086A0-FAAF-4CD4-9A0F-CD5E8D287270/Documents

This is the exact behavior that I'm seeing. I'm not really sure how this relates to getting a permission denied error and what I can do to fix it. It does say below -

To read and write user preferences, use the NSUserDefaults class or the CFPreferences API. These interfaces eliminate the need for you to construct a path to the Library/Preferences/ directory and read and write preference files directly. For more information on using these interfaces, see “Adding the Settings Bundle.”

If your application contains sound, image, or other resources in the application bundle, you should use the NSBundle class or CFBundle opaque type to load those resources. Bundles have an inherent knowledge of where resources live inside the application. In addition, bundles are aware of the user’s language preferences and are able to choose localized resources over default resources automatically. For more information on bundles, see “The Application Bundle.”

I don't see how I can use the Application Bundle to load bytes of data though. Any help or examples?

+1  A: 

The paragraph related to the application bundle refers to:

NSString *path = [[NSBundle mainBundle] pathForResource:@"somefile" ofType:@"png"];

This and other methods from NSBundle allow you to refer resources from inside the application bundle without actually knowing where the application bundle is located.

Disclaimer: I haven't worked with the iPhone.

With the disclaimer in mind, in plain OS X it's considered bad form to write stuff inside the application bundle. You save stuff under the user's Application Support directory -> see NSApplicationSupportDirectory.

diciu
A: 

Not sure how this works, but apparently using stringByAppendingPathComponent instead of stringByAppendingString for path creation fixed the problem.

bpapa