views:

411

answers:

2

Hi there, I've got a simple app that's trying to save some data between invocations using NSKeyedArchiver/NSKeyedUnarchiver. It works fine on the simulator, but when I export it to real iPhone hardware, NSKeyedArchiver fails to be able to write to disk.

I'm using the class method

[NSKeyedArchiver archiveRootObject:myRootObject toFile:@"data.archive"]

All my objects in 'myRootObject' implement the NSCoding protocol. Again, on the simulator, this returns YES and everything's good. On the device, this returns NO and nothing has saved. (Both are 2.2.1)

Is there some write permission or something an app needs to be able to write to the phone's HD? Anybody already seen this?

Thanks for your help in advance.

+6  A: 

I'm guessing here but wouldn't you specify file path pointing to directory within your bundle? I think you can write to Documents directory under your NSBundle.

Try creating the file path like this:

// Documents directory
NSArray *paths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                    NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];

// <Application Home>/Documents/foo.arch
NSString *fooPath =
[documentsPath stringByAppendingPathComponent:@“foo.arch”];

And then pass "fooPath" as the path to your file in the method you're using, see if that helps.

stefanB
A: 

Thanks for your help you sent me down the right path of thought.... the trick was to figure out the application's root dir, and write to a subdir of that. annoying that the simulator let's you write anywhere.

Here's what fixed all:

NSString *localPath = @"Documents/my.archive";
NSString *fullPath = [NSHomeDirectory() stringByAppendingPathComponent:localPath];
[NSKeyedArchiver archiveRootObject:archive toFile:fullPath];

See also this similar thread

Mike Fogel
Good that you figured out, I think the NSSearchPathForDirectoriesInDomains will give you your Documents directory as well.
stefanB