18

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

loganathan
  • 2,056
  • 2
  • 23
  • 34

3 Answers3

34

I just made my code work by changing the following code

NSString *sharedContainerPathLocation = [[self getSharedContainerURLPath] absoluteString];

to

NSString *sharedContainerPathLocation = [[self getSharedContainerURLPath] path];    
loganathan
  • 2,056
  • 2
  • 23
  • 34
2

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.

Hardik Thakkar
  • 15,269
  • 2
  • 94
  • 81
0

@Hardik Thakkar code for Swift 5 This function create directory in shared app group container and return path to it.

func createProjectDirectoryPath(path:String) -> String
{
    let containerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: Constants.Notifications.appGroupIdentifier)
    let logsPath = containerURL!.appendingPathComponent(path)
    //print(logsPath.path);

    do {
        try FileManager.default.createDirectory(atPath: logsPath.path, withIntermediateDirectories: true, attributes: nil)
    } catch let error as NSError {
        print("Unable to create directory \(error.debugDescription)")
    }
    return logsPath.path
}
Dmih
  • 530
  • 6
  • 9
  • Please put your answer always in context instead of just pasting code. See [here](https://stackoverflow.com/help/how-to-answer) for more details. – gehbiszumeis Nov 14 '19 at 07:02