iosobjective-cswiftcocoa

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?


Solution

  • 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'