Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically get path to Application Support folder

I'm trying to get an NSString for the user's Application Support folder.

I know I can do NSString *path = @"~/Library/Application Support"; but this doesn't seem very elegant. I've played around with using NSSearchPathForDirectoriesInDomains but it seems to be quite long-winded and creates several unnecessary objects (at least, my implementation of it does).

Is there a simple way to do this?

like image 763
Jack James Avatar asked Dec 08 '11 12:12

Jack James


4 Answers

This is outdated, for current best practice use FileManager.default.urls(for:in:) as in the comment by @andyvn22 below.

the Best practice is to use NSSearchPathForDirectoriesInDomains with NSApplicationSupportDirectory as "long winded" as it may be.

Example:

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

NSLog output:

applicationSupportDirectory: '/Volumes/User/me/Library/Application Support' 
like image 128
zaph Avatar answered Oct 06 '22 00:10

zaph


Swift:

print(NSHomeDirectory()) 

or

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

and

let yourString = String(FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first) 
like image 27
Juan Boero Avatar answered Oct 06 '22 00:10

Juan Boero


Swift 3:

FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first
like image 30
Andreas Ley Avatar answered Oct 06 '22 00:10

Andreas Ley


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

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

Expanded example from documentation:

- (NSURL*)applicationDataDirectory {
    NSFileManager* sharedFM = [NSFileManager defaultManager];
    NSArray* possibleURLs = [sharedFM URLsForDirectory:NSApplicationSupportDirectory
                                 inDomains:NSUserDomainMask];
    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: https://developer.apple.com/library/ios/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/AccessingFilesandDirectories/AccessingFilesandDirectories.html#//apple_ref/doc/uid/TP40010672-CH3-SW3

like image 35
Ivan Karpan Avatar answered Oct 06 '22 01:10

Ivan Karpan