3

I'm using UIDocumentPickerViewController to allow the user to select a folder in which to save a text file. All is working except for being able to display a message confirming the location of the saved file.

The screenshot shows a typical list of folders displayed by UIDocumentPickerViewController that the user can select:

UIDocumentPickerViewController

The folder names displayed to the user are those expected. However, the URLs returned by UIDocumentPickerViewController differ from those displayed and are not suitable for use in a confirmation message:

.../data/Library/Mobile Documents/com~apple~CloudDocs
.../data/Library/Mobile Documents/com~apple~CloudDocs/Desktop
.../data/Library/Mobile Documents/com~apple~CloudDocs/Documents
.../data/Library/Mobile Documents/3L68KQB4HG~com~readdle~CommonDocuments/Documents
.../data/Library/Mobile Documents/com~apple~CloudDocs/Downloads
.../data/Library/Mobile Documents/iCloud~net~jermware~gjLists/Documents
.../data/Library/Mobile Documents/iCloud~is~workflow~my~workflows/Documents
.../data/Containers/Shared/AppGroup/EB502BF4-9272-466F-9F5D-0CA97E35E687/File Provider Storage

In some of the URLs I could use just the last path component e.g. Desktop, Documents, Downloads, but for some it's the second from last path component e.g. gjLists, and for others the path is completely different e.g. Documents by Readdle, Shortcuts and On My iPhone.

This makes it very difficult/impossible to reliably parse the URL to extract the folder displayed to the user by UIDocumentPickerViewController.

After some investigation, I found the FileManager.default.displayName API which returns the display name of the file or directory at a specified path.

This improves things somewhat, giving the following folder names for the above URLs:

iCloud Drive
iCloud Drive
iCloud Drive
Documents by Readdle
Downloads
gjLists
Shortcuts
File Provider Storage

I now have the correct names for Documents by Readdle, Downloads, gjLists and Shortcuts, but just iCloud Drive for Desktop and Documents and still 'File Provider Storage' for 'On My Phone'.

Does anyone know how, given the URLs returned by UIDocumentPickerViewController, I can get the folder name as displayed in the picker? Displaying a confirmation message to the user saying the file was successfully saved to the 'File Provider Storage' folder rather than 'On My iPhone' is going to cause no end of confusion.

gjerman
  • 46
  • 1
  • 1
    So you are using a method which it’s intent is to import files to allow the user to pick a folder to save a file? If so you are using the wrong method. What you are looking for is UIDocumentInteractionController https://stackoverflow.com/questions/46456481/how-to-write-a-file-to-a-folder-located-at-apples-files-app-in-swift/46457518#46457518 – Leo Dabus Jul 13 '20 at 14:17
  • 1
    IMO you should always save your file locally and allow the user to export the file if he wants to. I don’t think a confirmation is necessary. – Leo Dabus Jul 13 '20 at 14:29
  • @LeoDabus (Offtopic for single files, anyway) Well, there is a valid use case for misusing the `UIDocumentPickerViewController` to select a folder: if the user is going to store multiple files at the same time. The alternative, `UIActivityViewController`, is not able to set filenames for Data() objects, so "picking" a folder fills the gap. – Frederik Winkelsdorf Dec 30 '20 at 16:00
  • 1
    @FrederikWinkelsdorf yes you can select a folder but note that AFAIK every time you launch an App the url will path change (sandboxing). So I wouldn't count on it for later usage. – Leo Dabus Dec 30 '20 at 16:07
  • @LeoDabus Absolutely right, this is better used as a one-time selection. Good catch! – Frederik Winkelsdorf Dec 30 '20 at 16:15
  • @LeoDabus It's possible to re-use an URL returned by a file picker: https://developer.apple.com/documentation/foundation/nsurl/1417795-bookmarkdata – Emma Labbé Jan 13 '21 at 03:14
  • @AdrianLabbé yes but that's a security-scoped bookmark. You can't simply save an URL. – Leo Dabus Jan 13 '21 at 03:20
  • @gjerman Did you ever find an answer to this question? I am facing this exact problem. – KeithTheBiped Nov 30 '22 at 16:23
  • 1
    @KeithTheBiped I moved my backup/restore functionality into a SwiftUI view and use the .fileExporter and .fileImporter methods to do what I need. Works well for my needs. – gjerman Dec 01 '22 at 17:30

0 Answers0