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?

有帮助吗?

解决方案

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

其他提示

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);
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top