Programmatically get path to Application Support folder

7 Solutions Collect From Internet About “Programmatically get path to Application Support folder”

Best practice is to use NSSearchPathForDirectoriesInDomains with NSApplicationSupportDirectory as “long winded” as it may be.


NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *applicationSupportDirectory = [paths firstObject];
NSLog(@"applicationSupportDirectory: '%@'", applicationSupportDirectory);

NSLog output:

applicationSupportDirectory: '/Volumes/User/me/Library/Application Support'




print(NSFileManager.defaultManager().URLsForDirectory(.ApplicationSupportDirectory, inDomains: .UserDomainMask)[0])


let yourString = String(NSFileManager.defaultManager().URLsForDirectory(.ApplicationSupportDirectory, inDomains: .UserDomainMask)[0])

Swift 3:

FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first

Just to be sure people will start using the recommended way of doing this:

- (NSArray<NSURL *> * _Nonnull)URLsForDirectory:(NSSearchPathDirectory)directory

Expanded example from documentation:

- (NSURL*)applicationDataDirectory {
    NSFileManager* sharedFM = [NSFileManager defaultManager];
    NSArray* possibleURLs = [sharedFM URLsForDirectory:NSApplicationSupportDirectory
    NSURL* appSupportDir = nil;
    NSURL* appDirectory = nil;

    if ([possibleURLs count] >= 1) {
        // Use the first directory (if multiple are returned)
        appSupportDir = [possibleURLs objectAtIndex:0];

    // If a valid app support directory exists, add the
    // app's bundle ID to it to specify the final directory.
    if (appSupportDir) {
        NSString* appBundleID = [[NSBundle mainBundle] bundleIdentifier];
        appDirectory = [appSupportDir URLByAppendingPathComponent:appBundleID];

    return appDirectory;

Proof link:

This works for me:

NSError *error;
NSURL* appSupportDir = [[NSFileManager defaultManager]     

This is what I use to get the database. Got it from the Stanford class. It might help somebody.

NSURL *url = [[[NSFileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
url = [url URLByAppendingPathComponent:@"database_name"];
NSLog(@"Database URL: %@",url);

Create separate objective C class for reading and writing into documents directory. I will avoid code re-writing. Below is my version of it.

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

#define PATH (NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES))
#define BASEPATH (([PATH count] > 0)? [PATH objectAtIndex:0] : nil)

@interface DocumentsDirectory : NSObject

//Here you can also use URL path as return type and file path.

#import "Directory.h"

@implementation DocumentsDirectory

UIAlertView *updateAlert;

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *filePath = [BASEPATH stringByAppendingPathComponent:filename];

    NSError *error;
    BOOL success = [fileManager removeItemAtPath:filePath error:&error]; //Remove or delete file from documents directory.

    if (success)
        updateAlert= [[UIAlertView alloc] initWithTitle:@"Congratulations:" message:@"File is updated successfully" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [updateAlert show];
        NSLog(@"Could not delete file -:%@ ",[error localizedDescription]);
        updateAlert= [[UIAlertView alloc] initWithTitle:@"Try again:" message:[error localizedDescription] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [updateAlert show];

    NSString *foldDestination = BASEPATH;
    NSString *filePath = [foldDestination stringByAppendingPathComponent:filename];

    return filePath;