Pergunta

I'n trying to save a simple .plist file to my documentsDirectory. I have followed Facebooks documentation for saving tokens and I have implemented the following methods:

static NSString* kFilename = @"TokenInfo.plist";

[...]

- (instancetype)init {
    if (self = [super init]) {
        _tokenFilePath = [self filePath];
    }

    return self;
}

- (NSString *)filePath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths lastObject];

    return [documentsDirectory stringByAppendingPathComponent:kFilename];
}

- (void)writeData:(NSDictionary *)data {

    NSLog(@"File = %@ and Data = %@", self.tokenFilePath, data);

    BOOL success = [data writeToFile:self.tokenFilePath atomically:YES];
    if (!success) {
        NSLog(@"Error writing to file");
    }
}

I currently get NSLog(@"Error writing to file"); printed very time. Any ideas on why that is?

Foi útil?

Solução

You want NSDocumentDirectory not NSDocumentationDirectory. Code completion in Xcode offers the documentation directory before the document directory enum. This is super easy to miss.

Outras dicas

Also check to Documents directory exist, it's safe to create it

Add this snippet to - (NSString *)filePath method before return

NSFileManager * fm = [NSFileManager defaultManager];
NSError * err;
[fm createDirectoryAtPath: documentsDirectory withIntermediateDirectories:YES attributes:nil error:&err];
if (err)
{
    NSLog(@"%@ ", err.localizedDescription);
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top