Search code examples
objective-cmacoscocoacachingpersistence

Get private/var/folders cache folder path with cocoa on mac os


I'm developing an app and I need to store some files on the macOs cache folder. Since the path changes for every user I cannot determine it. For exemple mine is:

/var/folders/_9/q7r9pg8s31g5m96jf7q3h3j80000gn/C/

I know that methods like

NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSAllDomainsMask, YES);

But it's only give me:

"/Users/me/Library/Caches" -- "/Library/Caches" -- "/System/Library/Caches"

Which is not I need. I'm looking for an equivalent to NSTemporaryDirectory() for cache folder.

The only way I found to get it is to use the terminal and type getconf DARWIN_USER_CACHE_DIR ...


Solution

  • You have to extend the Caches folder by adding a directory, like this:

    NSString *path = nil;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(
    NSCachesDirectory, NSUserDomainMask, YES);
    if ([paths count])
    {
        NSString *bundleName =
        [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"];
        path = [[paths objectAtIndex:0] stringByAppendingPathComponent:bundleName];
    }
    

    Based on https://www.cocoawithlove.com/2009/07/temporary-files-and-folders-in-cocoa.html