NSKeyedArchiver works in iPhone simulator, doesn't work on device

I have a simple application that tries to save some data between calls using NSKeyedArchiver / NSKeyedUnarchiver. It works great on a simulator, but when I export it to real iPhone hardware, NSKeyedArchiver will not be able to write to disk.

I use class method

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

All of my objects in 'myRootObject' implement the NSCoding protocol. Again, on the simulator, this returns YES and all is good. NO is returned on the device and nothing is saved. (Both are 2.2.1)

Is there any permission to record or something that the application should be able to record on an HD phone? Has anyone already seen this?

Thanks for your help in advance.

+4
source share
2 answers

I assume that here, but would you not indicate a file path pointing to a directory inside your package? I think you can write to the Documents folder under NSBundle.

Try creating the file path as follows:

// 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 use, see if that helps.

+10
source

Thanks for your help, that you sent me the right way of thinking ... the trick was to find out the root directory of the application and write a subdirectory to it. Annoying that the simulator allows you to write anywhere.

Here is what fixed everything:

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

See also a similar stream.

+1
source

Source: https://habr.com/ru/post/1285880/


All Articles