I have implemented iOS 11 Drag and Drop to allow PDF files to be dragged into my App from Files on iPad.
I have a DragDropFile class as follows:
import Foundation
import MobileCoreServices
//Drag and drop PDF files
class DragDropFile : NSObject, NSItemProviderReading {
let fileData:Data?
required init(data:Data, typeIdentifier:String) {
fileData = data
}
static var readableTypeIdentifiersForItemProvider: [String] {
var documentTypeArray: [String] = []
documentTypeArray = [kUTTypePDF as String]
return documentTypeArray
}
static func object(withItemProviderData data: Data, typeIdentifier: String) throws -> Self {
return self.init(data: data, typeIdentifier: typeIdentifier)
}
}
Then canHandle specifies this class:
func dropInteraction(_ interaction: UIDropInteraction, canHandle session: UIDropSession) -> Bool {
return session.canLoadObjects(ofClass: DragDropFile.self)
}
This works fine for dropping PDF files dragged from DropBox!
However, my App also needs access to the original PDF file name.
I would have expected to be able to also drop the URL as follows:
func dropInteraction(_ interaction: UIDropInteraction, canHandle session: UIDropSession) -> Bool {
return session.canLoadObjects(ofClass: NSURL.self) && session.canLoadObjects(ofClass: DragDropFile.self)
}
Unfortunately, this does not work with DropBox as there appears to be no associated URL.
Does anyone know how to drag and drop and access the file name?
Thanks!
item.itemProvider.loadDataRepresentation(forTypeIdentifier: kUTTypePDF as String, completionHandler: completionHandler)
results in the error: "The file “PDF document.pdf” couldn’t be opened because there is no such file." – colincameron