I am using following code to create a folder/file under the shared container path. Which will help both app extension and the extension containing app can access the data.
code to get the shared container url location:
+(NSURL*)getSharedContainerURLPath
{
NSFileManager *fm = [NSFileManager defaultManager];
NSString *appGroupName = APP_EXTENSION_GROUP_NAME; /* For example */
NSURL *groupContainerURL = [fm containerURLForSecurityApplicationGroupIdentifier:appGroupName];
return groupContainerURL;
}
code to create a directory
+(void)createDirAtSharedContainerPath
{
NSString *sharedContainerPathLocation = [[self getSharedContainerURLPath] absoluteString];
NSString *directoryToCreate = @"user_abc";
//basically this is <shared_container_file_path>/user_abc
NSString *dirPath = [sharedContainerPathLocation stringByAppendingPathComponent:directoryToCreate];
BOOL isdir;
NSError *error = nil;
NSFileManager *mgr = [[NSFileManager alloc]init];
if (![mgr fileExistsAtPath:dirPath isDirectory:&isdir]) { //create a dir only that does not exists
if (![mgr createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:&error]) {
NSLog(@"error while creating dir: %@", error.localizedDescription);
} else {
NSLog(@"dir was created....");
}
}
}
the above code not raising any error it says success but i am not able to find the folder under the shared container path. Any idea that might be appreciated
I just made my code work by changing the following code
NSString *sharedContainerPathLocation = [[self getSharedContainerURLPath] absoluteString];
to
NSString *sharedContainerPathLocation = [[self getSharedContainerURLPath] path];
For Swift
func createProjectDirectoryPath(path:String) -> String
{
let containerURL = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier("group.abc")
let logsPath = containerURL!.URLByAppendingPathComponent(path)
//print(logsPath.path);
do {
try NSFileManager.defaultManager().createDirectoryAtPath(logsPath.path!, withIntermediateDirectories: true, attributes: nil)
} catch let error as NSError {
NSLog("Unable to create directory \(error.debugDescription)")
}
return logsPath.path!
}
To Use
var strSavePath : String = self.createProjectDirectoryPath("Large")
Note: After your app group is setup this above code is useful to create folder.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With