Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/95.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS 8中每次启动时的文档或缓存路径更改_Ios_Ios8_Ios8.1 - Fatal编程技术网

iOS 8中每次启动时的文档或缓存路径更改

iOS 8中每次启动时的文档或缓存路径更改,ios,ios8,ios8.1,Ios,Ios8,Ios8.1,因为我正在我的应用程序中下载视频,并将其保存在本地缓存/文档路径中,并在必要时显示。它在iOS 7中工作,但avplayer在iOS 8及以上版本中不显示视频。正如我所读到的,在iOS 8中每次启动时,文档/缓存路径都会更改。问题是,我必须下载一次视频,然后在我的应用程序中多次播放。那个么,我怎样才能一次又一次地到达相同的路径,在应用程序中显示视频呢 Here is my code: NSArray *paths = NSSearchPathForDirectoriesInDomains(NS

因为我正在我的应用程序中下载视频,并将其保存在本地缓存/文档路径中,并在必要时显示。它在iOS 7中工作,但avplayer在iOS 8及以上版本中不显示视频。正如我所读到的,在iOS 8中每次启动时,文档/缓存路径都会更改。问题是,我必须下载一次视频,然后在我的应用程序中多次播放。那个么,我怎样才能一次又一次地到达相同的路径,在应用程序中显示视频呢

Here is my code:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// NSLog(@"Document folder: %@", paths);
NSString *documentsDirectory = [paths objectAtIndex:0];
   NSLog(@"Document folder: %@", documentsDirectory);

在日志中,我得到了每次发射的不同路径。任何帮助都将不胜感激。感谢

应用程序容器或沙盒更改的路径应该是预期的条件。您不应该将绝对文件系统路径存储到沙盒目录;而是存储相对于该目录的路径,并每次将其附加到
NSSearchPathForDirectoriesInDomains
的结果中。

我得到了答案。由于绝对路径在每次发射时都会发生变化,因此我们可以将数据保存在相对路径上,并在附加绝对路径和相对路径时检索数据

这就是我们如何将数据保存在相对路径上的方法:

NSString *documentsDirectory = @"MyFolder";
documentsDirectory = [documentsDirectory stringByAppendingPathComponent:filename];
NSString *relativePath = documentsDirectory;
  NSFileManager *fileManager = [NSFileManager defaultManager];
  NSString *fullCachePath = ((NSURL*)[[fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject] ).path;
  NSString *fullPath = [fullCachePath stringByAppendingPathComponent:relativePath];  
但在读取文件时,必须使用绝对路径+相对路径:

NSString *documentsDirectory = @"MyFolder";
documentsDirectory = [documentsDirectory stringByAppendingPathComponent:filename];
NSString *relativePath = documentsDirectory;
  NSFileManager *fileManager = [NSFileManager defaultManager];
  NSString *fullCachePath = ((NSURL*)[[fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject] ).path;
  NSString *fullPath = [fullCachePath stringByAppendingPathComponent:relativePath];  
对于数据库,也仅在相对路径上存储数据。但是,在读取时,使用绝对路径并附加来自数据库的相对路径进行读取。

它在这里工作。

建议使用书签引用文件系统资源

如果要永久保存文件位置,请使用NSURL的书签功能。书签是一种不透明的数据结构,包含在NSData对象中,用于描述文件的位置。虽然路径和文件引用URL在应用程序启动之间可能很脆弱,但书签通常可用于重新创建文件的URL,即使在文件被移动或重命名的情况下也是如此

书签提供对文件系统资源的持久引用。解析书签时,将获得指向资源当前位置的URL。如果用户移动或重命名资源,或者用户重新启动应用程序或重新启动系统,书签与文件系统资源(通常是文件或文件夹)的关联通常会继续工作

对于示例代码,请签出


如果您想直接使用URL实现向后兼容性,我有一个修复代码:

@interface NSURL (App)

/**
 This method try to rebuild a file url relative to application’s home directory.
 
 If the reciver is not a file url or not in the home directory. The original URL returns.
 
 @code
 
 NSString *homePath = NSHomeDirectory();
 // Assume "/Something/Application/B383551F-41C1-4E3D-8EA9-8D76E4AFA919"
 
 NSURL *test;
 test = [NSURL fileURLWithPath:homePath];
 [test URLByResolvingApplicationDirectoryChange];
 // file://Something/Application/B383551F-41C1-4E3D-8EA9-8D76E4AFA919
 
 test = [NSURL URLWithString:@"file:///Foo/bar"];
 [test URLByResolvingApplicationDirectoryChange];
 // file:///Foo/bar
 
 test = [NSURL URLWithString:@"file://Something/Application/12345678-1234-1234-1234-123456789ABC/path.tmp"];
 // file://Something/Application/B383551F-41C1-4E3D-8EA9-8D76E4AFA919/path.tmp
 
 @endcode
 */
- (nonnull NSURL *)URLByResolvingApplicationDirectoryChange;

@end


@implementation NSURL (App)

- (nonnull NSURL *)URLByResolvingApplicationDirectoryChange {
    if (!self.isFileURL) return self;

    NSString *pathHome = NSHomeDirectory();
    NSString *pathThis = self.path;
    if ([pathThis hasPrefix:pathHome]) {
        return self;
    }

    NSArray<NSString *> *cpHome = pathHome.pathComponents;
    NSMutableArray<NSString *> *cpThis = pathThis.pathComponents.mutableCopy;
    if (cpThis.count < cpHome.count) {
        return self;
    }

    NSInteger i = 0;
    for (i = 0; i < cpHome.count - 2; i++) {
        NSString *hp = cpHome[i];
        NSString *tp = cpThis[i];
        if (![hp isEqualToString:tp]) {
            return self;
        }
    }
    i++;
    NSString *hp = cpHome[i];
    NSString *tp = cpThis[i];
    if (hp.length != tp.length) {
        return self;
    }
    [cpThis replaceObjectAtIndex:i withObject:hp];
    NSString *resolvedPath = [NSString pathWithComponents:cpThis];
    return [NSURL.alloc initFileURLWithPath:resolvedPath];
}

@end
@接口NSURL(应用程序)
/**
此方法尝试重建相对于应用程序主目录的文件url。
如果接收器不是文件url或不在主目录中。原始URL返回。
@代码
NSString*homePath=nshomeditory();
//假设“/某物/应用程序/B383551F-41C1-4E3D-8EA9-8D76E4AFA919”
NSURL*试验;
test=[NSURL fileURLWithPath:homePath];
[通过分离应用程序目录更改来测试URL];
// file://Something/Application/B383551F-41C1-4E3D-8EA9-8D76E4AFA919
测试=[NSURL URLWithString:@”file:///Foo/bar"];
[通过分离应用程序目录更改来测试URL];
// file:///Foo/bar
测试=[NSURL URLWithString:@”file://Something/Application/12345678-1234-1234-1234-123456789ABC/path.tmp"];
// file://Something/Application/B383551F-41C1-4E3D-8EA9-8D76E4AFA919/path.tmp
@端码
*/
-(非空NSURL*)URLByResolvingApplicationDirectoryChange;
@结束
@实施NSURL(应用程序)
-(非空NSURL*)URLByResolvingApplicationDirectoryChange{
如果(!self.isFileURL)返回self;
NSString*pathHome=NSHomeDirectory();
NSString*pathThis=self.path;
if([pathThis hasPrefix:pathHome]){
回归自我;
}
NSArray*cpHome=pathHome.pathComponents;
NSMutableArray*cpThis=pathThis.pathComponents.mutableCopy;
if(cpThis.count
您正在访问应用程序的文档目录,而不是缓存目录。对于缓存,路径也在更改。每次启动时,“应用程序后路径”文件夹中的神秘数字都会更改。如果我使用NSCachesDirectory,这就是我在日志中得到的。这里的神秘数字也在变化/Users/mantavya/Library/Developer/CoreSimulator/Devices/76A9B09C-434E-4C29-879D-119556A10AA9/data/Containers/data/Application/34093128-B643-4030-B36A-350B3110C12C/Library/CachesI'v也注意到了这种行为。但是在官方文档中找不到任何参考。你能给我推荐一些链接吗?我必须在本地设备或模拟器上存储一些视频文件。保存文件时,你不需要指定完整路径,以便知道文件的去向吗?似乎最好的做法是在阅读和写作时都生成完整的路径。当我试图保存在相对路径中时,它并没有保存数据。它失败了。请您指导我如何在Documents目录下保存。您可以像上面两种方法一样进行存储。“文件夹名称”中的相对路径,保持原样。也可以复制下一部分,也可以。请添加保存和检索文件的代码,考虑文件是图像。