Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS8 Unable to create a folder/file under app group shared container location

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

like image 215
loganathan Avatar asked Sep 10 '14 09:09

loganathan


2 Answers

I just made my code work by changing the following code

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

to

NSString *sharedContainerPathLocation = [[self getSharedContainerURLPath] path];    
like image 151
loganathan Avatar answered Nov 10 '22 14:11

loganathan


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.

like image 33
Hardik Thakkar Avatar answered Nov 10 '22 15:11

Hardik Thakkar