Search code examples
iosmacosfopenipados

fopen() always return NULL on iOS device


File exists in iOS bundle directory. fopen() always return NULL on iOS device, but OK on iOS sumulator, codes as below:

NSString *path = [NSBundle.mainBundle pathForResource:@"yuv420p_640x360" ofType:@"yuv"];
BOOL exists = [NSFileManager.defaultManager fileExistsAtPath:path];
NSLog(@"%d", exists); //prints "1"
const char *path2 = [path UTF8String];
FILE *fp = fopen(path2, "rb+"); //fp is NULL

Environment: macOS 12.0.1, M1 MacBook Pro, iPadOS 15.0


Solution

  • The iOS execution environment is sandboxed, and you can not open a file in the bundle with "writing" mode, since the files in the bundle can not be modified (try with just "r" for the mode).