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))completionHandler
{
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;
...
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?
Thanks.
UPD: 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.
Looks like you are working on a security-scoped URL.
According to Document Picker Programming Guide
So you need to call startAccessingSecurityScopedResource before the file at this url is copied. Your code may become.