2

I'm writing an iOS 11 app where I want to be able to read an Excel file that is dropped onto the app from Files. I've added the drop interaction and canHandle session is called but I can't work out how to identify the UTI of the Excel file that is being dropped.

My code is:

func dropInteraction(_ interaction: UIDropInteraction, canHandle session: UIDropSession) -> Bool {
    // Ensure the drop session has an object of the appropriate type
    if let UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, "xls" as CFString, nil) {
        let UTIString = UTI.takeRetainedValue() as String
        let test = session.hasItemsConforming(toTypeIdentifiers: [UTIString]) && session.items.count == 1
        return test
    }
    return false
}

In this example UTIString resolves to a UTI string that looks like Excel but test is false when the Excel file is dropped and I can't see a way to work out what the UTI of the file that is being dropped actually is. Is there a way to interrogate the UIDropSession to find out?

Fleet Phil
  • 333
  • 2
  • 12
  • 3
    try `"com.microsoft.excel.xls"` for `.xls` files or `"org.openxmlformats.spreadsheetml.sheet"` if the pathExtension is `.xlsx` – Leo Dabus Dec 13 '17 at 20:55
  • 1
    Brilliant, thank you. I also managed to navigate my way to the answer how to find the type: `session.items[0].itemProvider.registeredTypeIdentifiers` – Fleet Phil Dec 13 '17 at 22:16
  • I recommend using URL resourceValues method and get the `.typeIdentifierKey` for the fileURLs that you don't know the UTI type. – Leo Dabus Dec 13 '17 at 22:19
  • Thanks, I will add that in. – Fleet Phil Dec 13 '17 at 23:31
  • https://stackoverflow.com/questions/28570627/how-to-find-file-uti-for-file-withouth-pathextension-in-a-path-in-swift/34772517#34772517 – Leo Dabus Dec 13 '17 at 23:31

0 Answers0