How to access the NSCachesDirectory directory in a shared folder?

Is there a way to access NSCachesDirectory in a shared folder for a container application and its extension?

NSURL *sharedDirectory = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:APPS_GROUP_NAME]; 

What is the correct way to get " /Library/Caches/ " after that?

+6
source share
1 answer

I found these two methods very useful. You must #define g_groupName for your common application group. CachesDirectory will first select the cache folder of the common application groups before returning to the folder of specific application caches. CachesDirectoryLocal always returns a folder of specific application caches. Hope this helps!

 #define g_groupName @"group.com.yourdomain.yourapp" #define DISPATCH_ONCE(var, code) static dispatch_once_t onceToken; var dispatch_once(&onceToken, ^{ code }); #define FILE_MANAGER ([[NSFileManager alloc] init]) NSString* CachesDirectory() { DISPATCH_ONCE(static NSString* s_cacheFolderForGroup = nil;, { if (g_groupName.length != 0) { s_cacheFolderForGroup = [FILE_MANAGER containerURLForSecurityApplicationGroupIdentifier:g_groupName].path; s_cacheFolderForGroup = [s_cacheFolderForGroup stringByAppendingPathComponent:@"Library/Caches/"]; [FILE_MANAGER createDirectoryAtPath:s_cacheFolderForGroup withIntermediateDirectories:YES attributes:nil error:nil]; } }); if (s_cacheFolderForGroup.length != 0) { return s_cacheFolderForGroup; } return CachesDirectoryLocal(); } NSString* CachesDirectoryLocal() { DISPATCH_ONCE(static NSString* s_cacheFolder = nil;, { NSArray* docDirs = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); if (docDirs.count != 0 && [[docDirs objectAtIndex:0] isKindOfClass:NSString.class]) { s_cacheFolder = (NSString*)[docDirs objectAtIndex:0]; if (![s_cacheFolder hasSuffix:@"/"]) { s_cacheFolder = [s_cacheFolder stringByAppendingString:@"/"]; } } else { s_cacheFolder = @"./Library/Caches/"; } }); return s_cacheFolder; } 
+1
source

Source: https://habr.com/ru/post/977310/


All Articles