0

My iOS 11 app works with cloud-based files using UIDocumentPickerViewController (in .open mode). Once I have the URL, how can I get the name of the location (file provider) that stores the file? (such as "iCloud", "Dropbox", etc)

My best guess was URLResourceValues.ubiquitousItemContainerDisplayName, but it seems to work only for iCloud.

Here's a test code:

func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
    guard let url = urls.first else { return }
    let isAccessible = url.startAccessingSecurityScopedResource()
    let res = try! url.promisedItemResourceValues(forKeys:     [.ubiquitousItemContainerDisplayNameKey])
    print("Container name: \(res.ubiquitousItemContainerDisplayName)")
    if isAccessible {
        url.stopAccessingSecurityScopedResource()
    }
}

For files picked from iCloud, it prints "iCloud Drive". But for Dropbox-based files the container name is nil.

Am I missing something? Is there another way?

anmipo
  • 1
  • 2

2 Answers2

0

This is not possible. Apple does not let you get this information to protect the user's privacy.

Thomas Deniau
  • 2,488
  • 1
  • 15
  • 15
0

This works with iOS 15 and probably much earlier:

func documentPicker(_ controller: UIViewControllerType, didPickDocumentsAt urls: [URL]) {
    guard let url = urls.first else { return }
    guard url.startAccessingSecurityScopedResource() else {
        print("unable to access security scoped resource: \(url.absoluteString)")
        return
    }
    defer { url.stopAccessingSecurityScopedResource() }

    let fileCoord = NSFileCoordinator.init()
    fileCoord.coordinate(readingItemAt: url, options: .immediatelyAvailableMetadataOnly, error: nil) { (url) in
        if let res = try? url.resourceValues(forKeys: [.ubiquitousItemContainerDisplayNameKey]),
           let name = res.ubiquitousItemContainerDisplayName {
               print("\(name)")
           } else {
               print("no name found")
           }
    }
}

You only get a result if the URL represents a location in iCloud Drive.

MichaelR
  • 1,681
  • 15
  • 28
  • Thank you. I had solved the original question by [parsing bookmark data](https://github.com/keepassium/KeePassium/blob/20864cd1bef61cf5e289c00511a145d5426e40ba/KeePassiumLib/KeePassiumLib/files/URLReference.swift#L512) directly — it happens to contain file provider ID in plain text. However, all the credit goes to Michael Lynn (aka pudquick) who wrote [Apple's BookmarkData - exposed!](http://michaellynn.github.io/2015/10/24/apples-bookmarkdata-exposed/) – anmipo Dec 22 '21 at 23:51
  • Sorry - I realise I didn't actually answer your question. Thank you for the links. Your solution is a hard road to travel! – MichaelR Dec 23 '21 at 00:21