What Is the Documents Directory (Nsdocumentdirectory)

What is the documents directory (NSDocumentDirectory)?

Your app only (on a non-jailbroken device) runs in a "sandboxed" environment. This means that it can only access files and directories within its own contents. For example Documents and Library.

See the iOS Application Programming Guide.

To access the Documents directory of your applications sandbox, you can use the following:

iOS 8 and newer, this is the recommended method

+ (NSURL *)applicationDocumentsDirectory
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}

if you need to support iOS 7 or earlier

+ (NSString *) applicationDocumentsDirectory 
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = paths.firstObject;
return basePath;
}

This Documents directory allows you to store files and subdirectories your app creates or may need.

To access files in the Library directory of your apps sandbox use (in place of paths above):

[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0]

What's the best way to find the user's Documents directory on an iPhone?

Objc:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)

Swift:

var paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)

You'll want the first element of the returned array.

iOS8 Document directory path

application Documents directory, it works on every iOS version:

- (NSURL *)applicationDocumentsDirectory {
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}

How to find NSDocumentDirectory in Swift?

Apparently, the compiler thinks NSSearchPathDirectory:0 is an array, and of course it expects the type NSSearchPathDirectory instead. Certainly not a helpful error message.

But as to the reasons:

First, you are confusing the argument names and types. Take a look at the function definition:

func NSSearchPathForDirectoriesInDomains(
directory: NSSearchPathDirectory,
domainMask: NSSearchPathDomainMask,
expandTilde: Bool) -> AnyObject[]!
  • directory and domainMask are the names, you are using the types, but you should leave them out for functions anyway. They are used primarily in methods.
  • Also, Swift is strongly typed, so you shouldn't just use 0. Use the enum's value instead.
  • And finally, it returns an array, not just a single path.

So that leaves us with (updated for Swift 2.0):

let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]

and for Swift 3:

let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]

Replace File in Documents Directory

You can check if your application is a fresh install or an update from AppDelegate. However, it seems you are not saving your App Version in NSUserDefaults, it will be a bit tricky for you. You save your new version of File.sqlite in Resource and copy it to document directory.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{
NSString* curVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];
NSString* lastVersion = [[NSUserDefaults standardUserDefaults] objectForKey:@"LastVersion"];

if ( lastVersion == nil )
{
//App Installed for the first time. In your case it might be an update because you do not store lastVersion value.

//Reference the path to the documents directory.
NSString *documentDir =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
//Get the path of old file.
NSString *filePath = [documentDir stringByAppendingPathComponent:@"File.sqlite"];

if([[NSFileManager defaultManager] fileExistsAtPath: filePath])
{
[fileManager removeItemAtPath:filePath error:&error] //Delete old file
}
//Copy New File.sqlite from Resource Bundle.
NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"File" ofType:@"sqlite"];
[fileManager copyItemAtPath:resourcePath toPath:filePath error:&error];
}
else if (![lastVersion isEqual: curVersion])
{
// App is updated. For future Versions you can Update files from here!
}

[[NSUserDefaults standardUserDefaults] setObject:curVersion forKey:@"LastVersion"];
[[NSUserDefaults standardUserDefaults] synchronize];
}

Hope this helps!

NSDocumentDirectory files disappear in ios

This is because in iOS 8 + the name of the Application folder is renamed each time you launch it.

Check it in /Users/"your username"/Library/Developer/CoreSimulator/Devices/"device name"/data/Containers/Data/Application/"application name" (Test in simulator).

So, you have to save the path without the document directory. And when you are trying to retrieve the path you have to add the document directory before the path you saved previously.

Like let your custom folder name is "Save_Video" and file name is "video_01.mp4".
Your file saving path will be "Application document directory"/Save_Video/video_01.mp4

Then you have to store only "Save_Video/video_01.mp4"(in Database/ NSUserDefaults) and when you are retrieving the file the path should be

"Application document directory"/Save_Video/video_01.mp4

Playing a video file saved in the app's documents directory

you can play Video from Document Directory like this way:-

-(IBAction)playVideo
{
NSURL *vedioURL;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil];
NSLog(@"files array %@", filePathsArray);

NSString *fullpath;

for ( NSString *apath in filePathsArray )
{
fullpath = [documentsDirectory stringByAppendingPathComponent:apath];
vedioURL =[NSURL fileURLWithPath:fullpath];
}
NSLog(@"vurl %@",vedioURL);
MPMoviePlayerViewController *videoPlayerView = [[MPMoviePlayerViewController alloc] initWithContentURL:vedioURL];
[self presentMoviePlayerViewControllerAnimated:videoPlayerView];
[videoPlayerView.moviePlayer play];
}

NOTE

Please note that don't Forget to include requested Framework or connect Delegate

How to get All paths for files in Documents directory?

This will give u the paths for all files under documents directory

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil];

NSLog(@"files array %@", filePathsArray);

You can get the path for each object in filePathsArray by using the below code

NSString *filePath = [documentsDirectory stringByAppendingPathComponent:[filePathsArray objectAtIndex:0]]; 


Related Topics



Leave a reply



Submit