Questions tagged [nsdocumentdirectory]

An iOS application concept to let applications access the file system from their sandboxed environment. Use this tag for questions/problems about how to use the NSDocumentDirectory from the application code.

iOS applications are run in a "sandboxed" environment. This means that they can only access files and directories within their own contents. The NSDocumentDirectory is what applications use to access their own documents directory.

See this question for more details: What is the documents directory (NSDocumentDirectory)?

Use this tag for questions/problems about how to use the NSDocumentDirectory from the application code.

373 questions
166
votes
10 answers

How to find NSDocumentDirectory in Swift?

I'm trying to get path to Documents folder with code: var documentsPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory:0,NSSearchPathDomainMask:0,true) but Xcode gives error: Cannot convert expression's type 'AnyObject[]!' to type…
Ivan R
  • 1,923
  • 5
  • 18
  • 15
135
votes
9 answers

What is the documents directory (NSDocumentDirectory)?

Can someone explain to me what the documents directory is on an iOS app and when to use it? Here is what I believe at present: To me, it seems to be a central folder where the user can store any files needed for the app. This would be a different…
user798719
  • 9,619
  • 25
  • 84
  • 123
115
votes
10 answers

Delete specified file from document directory

I want to delete an image from my app document directory. Code I have written to delete image is: -(void)removeImage:(NSString *)fileName { fileManager = [NSFileManager defaultManager]; paths =…
user1982396
71
votes
9 answers

How to create directory using Swift code (NSFileManager)

I'm having some trouble with converting Objective-C code to create a directory for Swift. Objective-C: NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths…
Gian
  • 1,193
  • 2
  • 8
  • 20
58
votes
9 answers

Delete files from directory inside Document directory?

I have created a Temp directory to store some files: //MARK: -create save delete from directory func createTempDirectoryToStoreFile(){ var error: NSError? let paths =…
user4790024
56
votes
7 answers

Is there a safer way to create a directory if it does not exist?

I've found this way of creating a directory if it does not exist. But it looks a bit wonky and I am afraid that this can go wrong in 1 of 1000 attempts. if(![[NSFileManager defaultManager] fileExistsAtPath:bundlePath]) { [[NSFileManager…
42
votes
3 answers

Save file in document directory in swift 3?

I am saving files in a document directory in swift 3 with this code: fileManager = FileManager.default // let documentDirectory = fileManager?.urls(for: .documentDirectory, in: .userDomainMask).first as String var path =…
TechChain
  • 8,404
  • 29
  • 103
  • 228
31
votes
8 answers

how to load image from local path ios swift (by path)

In my app I am storing an image in local storage and I am saving the path of that image in my database. How can I load the image from that path? Here is the code I am using in order to save the image: let myimage : UIImage = UIImage(data: data)! …
Jack.Right
  • 974
  • 3
  • 14
  • 30
31
votes
3 answers

CFURLSetResourcePropertyForKey failed when disable data backup on NSDocumentDirectory

I am trying to download image files and store in NSDocumentDirectory. In order to do so, I has to turn off data backup on iCloud and iTunes. Below are my codes: +(void)saveData:(NSData*)thedata:(NSString*)fileName { NSArray *paths =…
hook38
  • 3,899
  • 4
  • 32
  • 52
29
votes
4 answers

Document directory path change when rebuild application

I download the video file from url and save it in document directory with this path: let destination: DownloadRequest.DownloadFileDestination = { _, _ in let pathComponent = "pack\(self.packID)-\(selectRow + 1).mp4" let directoryURL:…
ava
  • 1,148
  • 5
  • 15
  • 44
23
votes
7 answers

Save Data to .plist File in Swift

I am trying to save data to a plist file in swift, but the data isn't showing up as it was saved when the plist is read. This is the code I was using. var documentsDirectory = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask,…
trumpeter201
  • 8,487
  • 3
  • 18
  • 24
22
votes
6 answers

Retrieve file creation or modification date

I'm using this piece of code to try to retrieve the last modified date of a file: NSError *error = nil; NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath: myFilePath error:&error]; if (attributes != nil) { …
Robert
  • 5,278
  • 43
  • 65
  • 115
20
votes
1 answer

max length of file name

i am using the code below to save an image in the NSDocumentDirectory -(BOOL)saveImage:(UIImage *)image name:(NSString *)name{ NSString *dir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; …
astazed
  • 649
  • 1
  • 10
  • 24
18
votes
2 answers

Read a .txt file inside documents directory

How can I read a .txt file inside documents directory? Can you show me code?
cyclingIsBetter
  • 17,447
  • 50
  • 156
  • 241
15
votes
5 answers

Remove all files from within documentDirectory in Swift

I am making an audio app, and the user can download files locally stored to the documentDirectory using FileManager. Next, I'd like to allow the user to delete all files using a button. In the documentation, there is a method to remove items. Here's…
darkginger
  • 652
  • 1
  • 10
  • 38
1
2 3
24 25