fopen() 在 iOS 设备上总是 return NULL

fopen() always return NULL on iOS device

文件存在于 iOS 包目录中。 fopen() 在 iOS 设备上总是 return NULL,但在 iOS sumulator 上正常,代码如下:

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

环境:macOS 12.0.1、M1 MacBook Pro、iPadOS 15.0

iOS执行环境是沙箱化的,你不能用“writing”模式打开bundle中的文件,因为bundle中的文件不能被修改(尝试只用“r”模式)。

您无法打开 iOS 中的任何文件。应用程序在 iOS 中 沙盒化 即应用程序只能访问其目录,不能访问(read/write)其目录之外的任何文件。