2016-01-16 53 views
0

我在苹果公司托管我的应用内购买内容。我已经设法下载内容,现在我想将它保存在设备中。iOS - NSFileManager文件不存在

[[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/[email protected] 
+0

你要把数据保存到哪里?你能打印'dstPath'并显示给我们吗? – Sulthan

+0

@Sulthan我已更新我的答案 – tnylee

+0

@Sulthan只要我不停止并再次运行应用程序,该文件就存在。 – tnylee

回答

2

的应用程序的沙箱的变化。您绝不能存储文件的完整路径。只存储一个相对路径。

就你而言,只存储相对于Documents文件夹的路径。当应用程序启动时重新加载相对路径时,重新计算完整路径。

+0

谢谢。它现在有效。 – tnylee

相关问题