Is possible get save documents file from my share extension?
I try to build the share extension, the share extension can get the file path. I write a funciton to save the file to my Documents folder.
I need to get the file from my main app. But I found I save the file from share extension path is
/var/mobile/Containers/Data/PluginKitPlugin/9EBF8983-E1DE-49E2-8589-CEE7305EB644/Documents/xxx.png
I using the main app to get the document path is below
/var/mobile/Containers/Data/Application/CFD67CD9-5A8A-473E-8ECF-FC1C1CF18098/Documents
the folder id is not same.
Is possible get the file from share extension saved file? Thank you very much.
You're right. Applications and extensions cannot share individual directories due to sandboxing limitations. That's why their directories are different – they're running in different processes.
Nevertheless, App Groups allow shared directories. You can access the App Group's directory by using the NSFileManager like this:
let groupPath = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.example.documents")
Any target matching the group entitlement will be able to read/write from/to the shared directory (or container). So, they aren't actually part of your application's or extension's process. It might also be interesting to take a look at iOS' File System Basics, in order to understand the architecture of App Group containers.
As suggested by others, I also highly recommend to read the App Extension Guide which explains the usage of containers quite well.
I try to write the code using objective-c in share extension.
In my share extension :
+(NSURL*)getSharedContainerURLPath
{
NSFileManager *fm = [NSFileManager defaultManager];
NSURL *groupContainerURL = [fm containerURLForSecurityApplicationGroupIdentifier:APP_GROUP];
return groupContainerURL;
}
-(void) saveFileAction{
NSString *docPath = [ClassName getSharedContainerURLPath].absoluteString;
NSString * fileNamePathString = [docPath stringByAppendingPathComponent:[self generateMyFileName]];
NSError *err;
BOOL fileSaveSuccess = [myFileData writeToFile: fileNamePathString
options:NSDataWritingFileProtectionComplete
error:&err];
}
Then my main App to load the folder data code:
NSString * folderPath = [ClassName getSharedContainerURLPath].absoluteString;
NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:folderPath error:NULL];
NSLog(@"directoryContent:%@",directoryContent);
I found the directory content always got the null.
I had set the APP Groups in the extension and main target.
My share extension save log file path below:
share extension trace log below:
write file success:0 / error:Error Domain=NSCocoaErrorDomain Code=4 "The file “0421_224358.png” doesn’t exist." UserInfo= {NSFilePath=file:/private/var/mobile/Containers/Shared/AppGroup/ACDE7845- E0E3-45C3-893F-6F6978EC3534/0421_224358.png, NSUnderlyingError=0x174251ac0 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}
My Main app trace the folder path below:
file:///private/var/mobile/Containers/Shared/AppGroup/ACDE7845-E0E3-45C3-893F-6F6978EC3534/
I don't known why it save error .
Have anyone can give some hint ? thank you very mch.
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