簡體   English   中英

iOS-NSFileManager文件不存在

[英]iOS - NSFileManager file not exist

我正在與Apple托管我的應用內購買內容。 我已經設法下載了內容,現在我想將其保存在設備中。

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

上面的代碼是我將下載的內容移動到所需位置的方式。

它似乎工作正常,當我嘗試顯示其內容時。 但是當我停止並再次運行該應用程序時,文件不存在。

我想念什么?

我的代碼:

- (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];
}

fullPathDst的示例是

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

應用的沙箱會在不同時間更改。 您絕不能存儲文件的完整路徑。 僅存儲相對路徑。

在您的情況下,僅存儲相對於Documents文件夾的路徑。 當應用啟動時重新加載相對路徑時,您會再次重新計算完整路徑。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM