Can't write to containerURLForSecurityApplicationGroupIdentifier?

In the following example, I have no write errors, but the data length is 0 when reading. Why can't I write to a 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;
}
+4
source share
1 answer

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

+8
source

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


All Articles