Ios NSFileManager createFileAtPath在模拟器中工作,但在设备上失败

Ios NSFileManager createFileAtPath在模拟器中工作,但在设备上失败,ios,ipad,nsfilemanager,Ios,Ipad,Nsfilemanager,我正试图在iOS上向磁盘写入一些图像数据,但尽管它在模拟器中工作得很好,但当我在真正的iPad上尝试时,它失败了,返回0 BOOL success = [[NSFileManager defaultManager] createFileAtPath:filePath contents:imageData attributes:nil]; 所讨论的路径如下所示:/Library/Caches/_0_0_0_0_1100_1149.jpg,我也尝试过/Documents/ 除了成功/失败之外,还有

我正试图在iOS上向磁盘写入一些图像数据,但尽管它在模拟器中工作得很好,但当我在真正的iPad上尝试时,它失败了,返回0

BOOL success = [[NSFileManager defaultManager] createFileAtPath:filePath contents:imageData attributes:nil];
所讨论的路径如下所示:/Library/Caches/_0_0_0_0_1100_1149.jpg,我也尝试过/Documents/


除了成功/失败之外,还有什么方法可以获取错误代码或其他信息吗?

如果您正在写入图像数据,为什么不尝试通过NSData写入,错误参数可以为您提供一些非常有用的提示,说明您的文件为什么没有写入。

如果您正在写入图像数据,为什么不尝试通过NSData写入,error参数可以为您提供一些非常有用的提示,说明文件不写入的原因。

结果表明,您必须以编程方式获取目录。iOS文件系统不像我预期的那样是沙盒

NSString* pathRoot = NSSearchPathForDirectoriesInDomains( NSCachesDirectory, NSUserDomainMask, YES )[0];

事实证明,您必须以编程方式获取目录。iOS文件系统不像我预期的那样是沙盒

NSString* pathRoot = NSSearchPathForDirectoriesInDomains( NSCachesDirectory, NSUserDomainMask, YES )[0];

模拟器不会模拟在设备上强制执行的文件系统沙箱。您可以在sim卡上的任何位置写入,但在设备上,除了指定的目录之外,任何位置写入都将失败


我猜你的路不知怎么走得不好。尝试记录您的路径以及从NSCachesDirectory获得的路径,如第二篇文章所示。它们几乎肯定是不同的。

模拟器不会模拟设备上强制执行的文件系统沙箱。您可以在sim卡上的任何位置写入,但在设备上,除了指定的目录之外,任何位置写入都将失败

我猜你的路不知怎么走得不好。尝试记录您的路径以及从NSCachesDirectory获得的路径,如第二篇文章所示。它们几乎肯定是不同的。

这是不合逻辑的:

if ( !( [ fileManager copyItemAtPath:resourceDBFolderPath toPath:documentDBFolderPath error:&error ])) 
您正在检查该方法是否存在,而不是它是否成功

我在我的项目中使用了一些相关的代码,我希望它能以某种方式帮助您:

-(void)testMethod {

    NSString *resourceDBFolderPath;

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,
                                                         NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *documentDBFolderPath = [documentsDirectory stringByAppendingPathComponent:@"plans.gallery"];
    BOOL success = [fileManager fileExistsAtPath:documentDBFolderPath];

    if (success){
        NSLog(@"Success!");
        return;
    } 
    else {
        //simplified method with more common and helpful method 
        resourceDBFolderPath = [[NSBundle mainBundle] pathForResource:@"plans" ofType:@"gallery"];

        //fixed a deprecated method
        [fileManager createDirectoryAtPath:documentDBFolderPath withIntermediateDirectories:NO attributes:nil error:nil];

        [fileManager copyItemAtPath:resourceDBFolderPath toPath:documentDBFolderPath           
                              error:&error];

        //check if destinationFolder exists
        if ([ fileManager fileExistsAtPath:documentDBFolderPath])
        {
            //FIXED, another method that doesn't return a boolean.  check for error instead
            if (error)
            {
                //NSLog first error from copyitemAtPath
                NSLog(@"Could not remove old files. Error:%@", [error localizedDescription]);

                //remove file path and NSLog error if it exists.
                [fileManager removeItemAtPath:documentDBFolderPath error:&error];
                NSLog(@"Could not remove old files. Error:%@", [error localizedDescription]);
                return;
            }
        }
    }
}
这是不合逻辑的:

if ( !( [ fileManager copyItemAtPath:resourceDBFolderPath toPath:documentDBFolderPath error:&error ])) 
您正在检查该方法是否存在,而不是它是否成功

我在我的项目中使用了一些相关的代码,我希望它能以某种方式帮助您:

-(void)testMethod {

    NSString *resourceDBFolderPath;

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,
                                                         NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *documentDBFolderPath = [documentsDirectory stringByAppendingPathComponent:@"plans.gallery"];
    BOOL success = [fileManager fileExistsAtPath:documentDBFolderPath];

    if (success){
        NSLog(@"Success!");
        return;
    } 
    else {
        //simplified method with more common and helpful method 
        resourceDBFolderPath = [[NSBundle mainBundle] pathForResource:@"plans" ofType:@"gallery"];

        //fixed a deprecated method
        [fileManager createDirectoryAtPath:documentDBFolderPath withIntermediateDirectories:NO attributes:nil error:nil];

        [fileManager copyItemAtPath:resourceDBFolderPath toPath:documentDBFolderPath           
                              error:&error];

        //check if destinationFolder exists
        if ([ fileManager fileExistsAtPath:documentDBFolderPath])
        {
            //FIXED, another method that doesn't return a boolean.  check for error instead
            if (error)
            {
                //NSLog first error from copyitemAtPath
                NSLog(@"Could not remove old files. Error:%@", [error localizedDescription]);

                //remove file path and NSLog error if it exists.
                [fileManager removeItemAtPath:documentDBFolderPath error:&error];
                NSLog(@"Could not remove old files. Error:%@", [error localizedDescription]);
                return;
            }
        }
    }
}

请不要在没有留下建设性意见的情况下否决投票。这不是YouTube。什么是文件路径?它是如何获得的?它只是构造的。这似乎确实是个问题。显然,/Library/Caches/目录实际上不在/Library/Caches/。误导性文档。您无法在iOS设备上访问/Library/Caches/。除非文件路径在你的应用程序目录中,否则你不能在不越狱的情况下写入它。我也对没有解释就否决问题的原因感到沮丧+法官们,请不要在没有留下建设性意见的情况下否决投票。这不是YouTube。什么是文件路径?它是如何获得的?它只是构造的。这似乎确实是个问题。显然,/Library/Caches/目录实际上不在/Library/Caches/。误导性文档。您无法在iOS设备上访问/Library/Caches/。除非文件路径在你的应用程序目录中,否则你不能在不越狱的情况下写入它。我也对没有解释就否决问题的原因感到沮丧+法官们也这样做了,但结果是一样的。我想也许这条路错了?我也试过了,但结果是一样的。我想也许这条路错了?