文档或缓存路径更改iOS 8中的每次启动 [英] Document Or cache Path Changes on every launch in iOS 8

查看:150
本文介绍了文档或缓存路径更改iOS 8中的每次启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在我的应用程序中下载视频并将其保存在本地缓存/文档路径中并在必要时显示。它在iOS 7中工作但是avplayer没有在iOS 8及更高版本中显示视频。正如我已经读到的那样,iOS 8中的每次启动都会更改文档/缓存路径。问题是,我必须下载一次视频并在我的应用中多次显示。那么我怎么能一次又一次地到达相同的路径来在应用程序中显示视频。

As i am downloading a video in my app and keeping it in local cache/Document path and showing when necessary. It is working in iOS 7 but the avplayer not showing video in iOS 8 and above. As i have read that the document/cache path is changed on every launch in iOS 8. The issue is, I have to download video once and show it multiple times in my app. So how can i reach the same path again and again to show video in app.

Here is my code:

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

在日志中,我在每次启动时获得不同的路径。任何帮助,将不胜感激。谢谢

In The log I am getting different path on each launch. Any Help would be appreciated. Thanks

推荐答案

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

I got the answer. As the absolute path is changing on every launch, we can save the data on relative path and retrieve it on appending absolute path and relative path.

这是我们如何在相对路径上保存数据:

This is how we can save the data on the relative path:

NSString *documentsDirectory = @"MyFolder";
documentsDirectory = [documentsDirectory stringByAppendingPathComponent:filename];
NSString *relativePath = documentsDirectory;

但是当你读取文件时,你必须使用绝对路径+相对路径:

But when you read the file you have to use absolute path + relative path:

  NSFileManager *fileManager = [NSFileManager defaultManager];
  NSString *fullCachePath = ((NSURL*)[[fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject] ).path;
  NSString *fullPath = [fullCachePath stringByAppendingPathComponent:relativePath];  

对于数据库,还仅在相对路径上存储数据。但是读取时采用绝对路径并附加来自数据库和读取的相对路径。

它在这里工作。

For Database also store data on the relative path only. But while reading take the absolute path and append the relative path coming from database and read.
Its Working here.

这篇关于文档或缓存路径更改iOS 8中的每次启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆