Reputation: 2086
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
Upvotes: 18
Views: 7423
Reputation: 626
@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
}
Upvotes: 0
Reputation: 15991
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.
Upvotes: 2
Reputation: 2086
I just made my code work by changing the following code
NSString *sharedContainerPathLocation = [[self getSharedContainerURLPath] absoluteString];
to
NSString *sharedContainerPathLocation = [[self getSharedContainerURLPath] path];
Upvotes: 34