Search code examples
objective-ctvosnskeyedarchiver

NSKeyedArchiver works on simulator but fails on tvOS device


I'm archiving an object to a file - on simulator it works perfectly, but on a tvOS (Apple TV) device it doesn't (in other words -(BOOL)archiveRootObject:toFile: returns a funny NO):

static NSString * _DocumentsDirectory() {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentsDirectory, NSUserDomainMask, YES);
    return paths[0];
}

and:

NSString *fullPath = [_DocumentsDirectory() stringByAppendingPathComponent:filename];
[NSKeyedArchiver archiveRootObject:obj toFile:fullPath];

where filename = @"foo.data" and obj conforms to NSCoding.

I tried different ways to put together the path and different directories, but the result is the same. The only thought I have is that there could be some writing permission I need to set.

Any idea?


Solution

  • After investigating a bit, I found out that the only ways that Apple allows you to store data in tvOS apps are the following:

    enter image description here

    Therefore, I opted for using the Cache directory (which is purged only if space is needed) for data that I can eventually re-download, and NSUserDefaults for smaller data which I need to always access locally.