File Provider Extension, importDocumentAtURL:: can't read file at given URL (iOS 11.4.1)

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:&copyError];
    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.

Answered by DTS Engineer in 768685022

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.

You need to call


[fileURL startAccessingSecurityScopedResource];


before accessing the file, and


[fileURL stopAccessingSecurityScopedResource];


once you're done.

``startAccessingSecurityScopedResource``

Um

Hi

fileURLmyFileURLcopyErrorNSStringcopyErrorMessagecopyError.localizedDescription

I love

I love

thank you

Wow thank you

I can’t access any of MY files like this bc they have private info info them from someone else.

file:///var/mobile/Containers/Data/Application/0C43235D-B792-47DB-B7FD-CC4E7E1A6D32/Library/Caches/GBTNetworkFileCacheDirectory/GBTAttachmentCache.MTEzOTY5MDIwODM3MTMwNzU2NDA3/6D91F705-B6B0-415A-81ED-E96AC78CE0B8/Burratta-Espuma-2.pdf

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.

file:///private/var/mobile/Containers/Shared/AppGroup/770142B4-3D2A-4A99-AA4C-FC79D11A7194/File%20Provider%20Storage/Downloads/takeout-20240710T004949Z-003.zip.download

File Provider Extension, importDocumentAtURL:: can't read file at given URL (iOS 11.4.1)
 
 
Q