I'm having troubles with Paste opertaions into my containers in File Provider extension.
If I paste copied image or text into Files app -> My App -> any folder the file at fileURL can not be read (as a result can't be uploaded to my servers nor stored locally).
- (void)importDocumentAtURL:(NSURL *)fileURL
toParentItemIdentifier:(NSFileProviderItemIdentifier)parentItemIdentifier
completionHandler:(void (^)(NSFileProviderItem _Nullable importedDocumentItem, NSError * _Nullable error))completion
{
NSError *readError = nil;
NSData *fileData = [NSData dataWithContentsOfURL:fileURL options:NSDataReadingMappedAlways error:&readError];
NSString *readErrorMessage = readError.localizedDescription;
NSURL *myFileURL = [NSFileProviderManager.defaultManager.documentStorageURL URLByAppendingPathComponent:@"temp.dat"];
NSError *copyError = nil;
BOOL copyResult = [_fileManager copyItemAtURL:fileURL toURL:myFileURL error:©Error];
NSString *copyErrorMessage = copyError.localizedDescription;
...
copyResult is false, both readErrorMessage and copyErrorMessage are:
The file “text.txt” couldn’t be opened because you don’t have permission to view it.
What am I doing wrong here?
This happens to any file copied from my container, iCloud container, as well as synthetic files produced from text/image/other data from system Clipboard.
For emalple, fileURL for image.png file I'm trying to duplicate from my own container is
file:///private/var/mobile/Containers/Data/Application/80480715-574A-4E99-B588-FBDCC7F1FDFC/tmp/DDBD7C7B-56C0-4F61-94A9-109613C78F75/image.png/
The same time the item being duplicated is located at:
file:///private/var/mobile/Containers/Shared/AppGroup/A8C1416D-8435-407E-82B3-CE10A8439630/File%20Provider%20Storage/Document93838429/image.png
I guess I cant ready anything in /private/var/mobile/Containers/Data/Application/80480715-574A-4E99-B588-FBDCC7F1FDFC/
Thanks.
As of today, a non-replicated file provider, or NSFileProviderExtension, on both macOS and iOS, has evolved to a replicated file provider, or NSFileProviderReplicatedExtension, and importDocument(at:toParentItemIdentifier:completionHandler:)
isn't part of a replicated file provider. If you haven’t adopted replicated file providers, now is the time, and here is a well-written sample to show you how to do that: Synchronizing files using file provider extensions.
If you need to stick with NSFileProviderExtension
for some reason, yes, use start/stopAccessingSecurityScopedResource()
to access the URL the system passes to importDocument(at:toParentItemIdentifier:completionHandler:)
. This is documented in the API specification of the method.