Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Document Or cache Path Changes on every launch in iOS 8

Tags:

ios

ios8

ios8.1

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

like image 743
Developer007 Avatar asked Nov 18 '14 06:11

Developer007


1 Answers

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.

like image 130
Developer007 Avatar answered Oct 19 '22 23:10

Developer007