Home > Net >  Why all these ShareSheet errors?
Why all these ShareSheet errors?

Time:11-25

Related to enter image description here

When I open a file using QLPreviewController, and press the "Share" button, the console prints the following logs:

[cloudthumbnails.generation] Unable to determine whether file:///Users/xxxxxx/Library/Developer/CoreSimulator/Devices/XXXXXX/data/Containers/Data/Application/XXXXXX/Documents/xxxxxx.pdf is a directory: Error Domain=NSCocoaErrorDomain Code=260

[ShareSheet] Failed to request default share mode for fileURL:file:///Users/xxxxxx/Library/Developer/CoreSimulator/Devices/XXXXXX/data/Containers/Data/Application/XXXXXX/Documents/xxxxxx.pdf

[ShareSheet] Only support loading options for CKShare and SWY types.

[ShareSheet] error fetching item for URL:file:///Users/xxxxxx/Library/Developer/CoreSimulator/Devices/XXXXXX/data/Containers/Data/Application/XXXXXX/Documents/xxxxxx.pdf

[ShareSheet] error loading metadata for documentURL:file:///Users/xxxxxx/Library/Developer/CoreSimulator/Devices/XXXXXX/data/Containers/Data/Application/XXXXXX/Documents/xxxxxx.pdf

But, the path is correct (in the app's Documents folder), and the file can be opened and shared successfully by any means: save on my Documents, Gmail, AirDrop, Slack, etc.

Then, why all these errors? I believe the following crash I am receiving on Firebase Crashlytics may be related:

-[UIDocumentPickerViewController initForExportingURLs:asCopy:] must be called with a URL pointing to an existing file: Error Domain=NSCocoaErrorDomain Code=260 UserInfo (NSURL=file:///var/mobile/Containers/Data/Application/APP_ID/Documents/FILE.pdf, NSFilePath=/var/mobile/Containers/Data/Application/APP/ Documents/FILE.pdf, (Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"))

Because I am not calling [UIDocumentPickerViewController initForExportingURLs:asCopy:] elsewhere in the app. The only way that method gets called is by sharing a file from the app and storing it in the iPhone file system.

CodePudding user response:

The problem turned out to be the following.

All the files were being removed for security when the app moved to the background, and applicationDidEnterBackground(application:) was called on the AppDelegate.

I was confused because the file I was viewing using QLPreviewController was still there when I came from the background, and I didn't realize it was being deleted.

How to reproduce the error?

  1. Visualize a file using QLPreviewController.
  2. Save that file into iPhone's local files (this will call [UIDocumentPickerViewController initForExportingURLs:asCopy:]).
  3. Send the app to the background (or any other action that deletes the file).
  4. Come back to the foreground, the file is still open, and you can visualize it, but it doesn't exist on the expected path because it has been deleted.
  5. Try again to save that file into iPhone's local files (this will call [UIDocumentPickerViewController initForExportingURLs:asCopy:]) again.

Following these steps will cause the app to crash because the second time we try to export the file, it doesn't exist in the expected path even though it is still open and visible on QLPreviewController.

Solution

Download the file again from its URL when we come from the background (as it's no anymore available locally). To do that, subscribe to the didBecomeActive notification on the QLPreviewController.

notificationCenter.addObserver(self, selector: #selector(self.downloadFile), name: UIApplication.didBecomeActiveNotification, object: nil)
  • Related