NSDocumentDirectory文件在ios中消失

Cra*_*Dev 4 file ios nsdocumentdirectory ios8

我想在我的文件夹中保存一个mp4视频但是当我再次打开应用程序时,这个文件是零.但是当我保存文件时,我可以打开它,所以它似乎从文件夹中消失了.

保存:

NSData *videoData = [NSData dataWithContentsOfURL:exportUrl];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *tempPath = [documentsDirectory stringByAppendingFormat:@"/%@",videoName];

self.path_video_to_save = tempPath;

BOOL success = [videoData writeToFile:tempPath atomically:YES];

if (success) 
    NSLog(@"saved");
else
    NSLog(@"not saved!!!!!!!!!!!!!!");
Run Code Online (Sandbox Code Playgroud)

我获得了成功,所以没关系,我可以很好地播放我的视频.

NSString *path_video = [dict objectForKey:@"path"]; //dictionary where I save the path, the same before and after closing app

NSData *videoData = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:path_video]];

if (videoData == nil){
    NSLog(@"DATA NULL");
}
else
    NSLog(@"DATA OK");

    NSLog(@"PATH:%@", path_video);

    self.player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:path_video]];
Run Code Online (Sandbox Code Playgroud)

在这一点上它工作正常.

但是,当我关闭并再次打开应用程序并且我得到路径时,我的应用程序崩溃并且我有日志" DATA NULL"我不明白为什么当我关闭我的应用程序时文件消失...怎么了?

谢谢

Sk.*_*zad 7

这是因为在iOS 8 +中,每次启动时都会重命名Application文件夹的名称.

检查/ Users /"您的用户名"/ Library/Developer/CoreSimulator/Devices /"设备名称"/ data/Containers/Data/Application /"应用程序名称"(在模拟器中测试).

因此,您必须保存没有文档目录的路径.当您尝试检索路径时,必须在先前保存的路径之前添加文档目录.

比如让您的自定义文件夹名称为"Save_Video",文件名为"video_01.mp4".您的文件保存路径将是"应用程序文档目录"/Save_Video/video_01.mp4

然后你只需要存储"Save_Video/video_01.mp4"(在Database/NSUserDefaults中),当你检索文件时,路径应该是

"申请文件目录"/Save_Video/video_01.mp4