NSDocumentDirectory文件在ios中消失

我想保存在我的文件夹中的MP4video,但是当我再次打开应用程序,这个文件是零。 但是,当我保存文件,我可以打开它,所以它似乎从文件夹中消失。

保存:

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!!!!!!!!!!!!!!"); 

我真的获得了成功,所以没关系,我可以很好地播放我的video。

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

并在这一点上工作正常。

但是,当我closures并再次打开应用程序,我得到的path,我的应用程序崩溃,我有日志“ DATA NULL ”我不明白为什么当我closures我的应用程序文件消失…怎么了?

谢谢

这是因为在iOS 8 +中,每次启动应用程序文件夹的名称都会被重命名。

在/ Users /“你的用户名”/ Library / Developer / CoreSimulator / Devices /“device name”/ data / Containers / Data / Application /“application name”(Test in simulator)中检查。

所以,你必须保存没有文档目录的path。 而当您尝试检索path时,您必须在先前保存的path之前添加文档目录。

就像让你的自定义文件夹名称是“Save_Video”,文件名是“video_01.mp4”。 您的文件保存path将是“应用程序文档目录”/Save_Video/video_01.mp4

那么你只需要存储“Save_Video / video_01.mp4”(在数据库/ NSUserDefaults),当你检索文件的path应该是

“应用程序文档目录”/Save_Video/video_01.mp4