Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get URL for application's document directory iPhone

Tags:

iphone

This code is a part of Core Data. The URLsForDirectory....method does not run on iOS < 4 so I need to know some other methods/objects to call. I would also appriciate documentation for future reference. Thank you

/**  Returns the URL to the application's Documents directory. */ - (NSURL *)applicationDocumentsDirectory {     return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; } 
like image 664
LuckyLuke Avatar asked Apr 09 '11 21:04

LuckyLuke


People also ask

How do I access the Documents folder on my Iphone?

Tap Browse at the bottom of the screen, then tap an item on the Browse screen. If you don't see the Browse screen, tap Browse again. To view recently opened files, tap Recents at the bottom of the screen. To open a file, location, or folder, tap it.

What is document directory in iOS?

Every iOS app gets a slice of storage just for itself, meaning that you can read and write your app's files there without worrying about colliding with other apps. This is called the user's documents directory, and it's exposed both in code (as you'll see in a moment) and also through iTunes file sharing.

What is a document directory?

The Document Directory is a tab to house supporting documentation for your accreditation or program review reports. There is on Directory per collection of reports, and this documentation can be used across all requirements/standards in within the particular collection.

How do I open the file manager in iOS Swift?

Do a print(path. absoluteString) and then $ open path in Terminal, to open the file or folder at path. Keep in mind that the directory (on your Mac) where a Simulator's files are stored can change between running your app.


1 Answers

An alternative solution would be to use the -[NSFileManager URLsForDirectory:inDomains:] method and fetching the path from the returned NSArray:

Objective-C:

NSArray *paths = [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask]; NSURL *documentsURL = [paths lastObject]; 

Swift:

let paths = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask) let documentsURL = paths[0] as! NSURL 

This is supported in iOS 4.0+

like image 142
Levi McCallum Avatar answered Sep 17 '22 13:09

Levi McCallum