iphone

How to get URL for application's document directory 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];
}

Solution

  • You can get the document path via the NSSearchPathForDirectoriesInDomains foundation function as follows:

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

    This will work on iOS 2+.