简体   繁体   English

iOS-NSFileManager文件不存在

[英]iOS - NSFileManager file not exist

I am hosting my in-app purchase content with Apple. 我正在与Apple托管我的应用内购买内容。 I have managed to download the contents and right now I want to save it in device. 我已经设法下载了内容,现在我想将其保存在设备中。

[[NSFileManager defaultManager] removeItemAtPath:dstPath error:nil];
[[NSFileManager defaultManager] copyItemAtPath:srcPath toPath:dstPath error:nil];

This code above is how I move the content downloaded to my desired location. 上面的代码是我将下载的内容移动到所需位置的方式。

It seems to work fine and when I try to display the content it works. 它似乎工作正常,当我尝试显示其内容时。 But when I stop and run the app again the files does not exist. 但是当我停止并再次运行该应用程序时,文件不存在。

What am I missing? 我想念什么?

My code: 我的代码:

- (void)processDownload:(SKDownload *)download
{
    NSString *path = [download.contentURL path];

    path = [path stringByAppendingPathComponent:@"Contents"];

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *files = [fileManager contentsOfDirectoryAtPath:path error:&error];

    NSMutableArray *stickersDownloadedArray = [[NSMutableArray alloc] init];

    for (NSString *file in files)
    {
        NSString *fullPathSrc = [path stringByAppendingPathComponent:file];

        NSString *fileName = [fullPathSrc lastPathComponent];
        NSString *fullPathDst = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@", fileName]];

        [fileManager removeItemAtPath:fullPathDst error:nil];
        [fileManager moveItemAtPath:fullPathSrc toPath:fullPathDst error:nil];

        NSLog(@"fullPathDst: %@", fullPathDst);
        NSLog(@"file: %@", file);
        NSLog(@"key: %@", download.transaction.payment.productIdentifier);

        [stickersDownloadedArray addObject:fullPathDst];
    }

    [[NSUserDefaults standardUserDefaults] setObject:stickersDownloadedArray
                                              forKey:download.transaction.payment.productIdentifier];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

An example of fullPathDst is fullPathDst的示例是

/var/mobile/Containers/Data/Application/E921ADD6-B022-4DA2-8416-627DB44E679A/Documents/real4_12@2x.png

An app's sandbox changes at different times. 应用的沙箱会在不同时间更改。 You must never store full paths to files. 您绝不能存储文件的完整路径。 Only store a relative path. 仅存储相对路径。

In your case, only store the path relative to the Documents folder. 在您的情况下,仅存储相对于Documents文件夹的路径。 When you reload the relative path when the app starts up, you recalculate the full path again. 当应用启动时重新加载相对路径时,您会再次重新计算完整路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM