Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSKeyedArchiver works in iPhone simulator, fails on device

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.

like image 295
Mike Fogel Avatar asked Dec 23 '22 11:12

Mike Fogel


1 Answers

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.

like image 97
stefanB Avatar answered Mar 06 '23 21:03

stefanB