Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot write to containerURLForSecurityApplicationGroupIdentifier?

In the following example, I get no errors on write, but while reading, the data length is 0. Why can't I write to the shared container?

+ (UIImage *)read {
    NSData *data = [NSData dataWithContentsOfFile:[self getPath]];
    NSLog(@"Data length: %d", data.length);
    UIImage *image = [UIImage imageWithContentsOfFile:[self getPath]];
    NSLog(@"%@", image);
    return image;
}

+ (void)write:(UIImage *)image {
    NSString *pngFilePath = [self getPath];
    NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(image)];
    [data1 writeToFile:pngFilePath atomically:YES];
}

+ (NSString *)getPath {
    NSURL *containerUrl = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.me.app"];
    NSString *pngFilePath = [[containerUrl absoluteString] stringByAppendingPathComponent:@"current.png"];

    NSLog(@"%@", pngFilePath);
    return pngFilePath;
}
like image 409
Stefan Kendall Avatar asked Feb 11 '23 05:02

Stefan Kendall


1 Answers

You're mixing up URLs and file paths. Either use [containerUrl path] instead of [containerUrl absoluteString], or use dataWithContentsOfURL/writeToURL instead of dataWithContentsOfFile/writeToFile.

like image 183
Bart Whiteley Avatar answered Feb 13 '23 21:02

Bart Whiteley