Reputation: 6893
I am new to iOS 11 Drag and Drop feature. I know how to load a particular type of object when dropped in view that can accept that type of object. However, before calling the loadObject(ofClass:completionHandler:)
, I want to know the values that the dropped item is holding.
In my dropInteraction(_:performDrop:)
delegate method, when I try to get the description of the itemProvider object, I only get the type-
func dropInteraction(_ interaction: UIDropInteraction, performDrop session: UIDropSession) {
if let itemProvider = session.items.last?.itemProvider{
print("item provider \(itemProvider.description)")
}
}
As I was dropping an image from safari, the above code printed-
item provider <UIItemProvider: 0x600000c43640> {types = (
"public.jpeg",
"public.url"
)}
Here I can't see the url value. If I want to get the url value, I have to do-
func dropInteraction(_ interaction: UIDropInteraction, performDrop session: UIDropSession) {
session.items.last?.itemProvider.loadObject(ofClass: URL.self, completionHandler: { itemProvider, err in
if let urlItemProvider = itemProvider{
print("url = \(urlItemProvider.absoluteString)")
}
})
}
Basically whenever an image is dropped, I want to save its associated url and the details about the image in a custom class before I attempt to load it. Any help will be appreciated.
Upvotes: 5
Views: 5546
Reputation: 2132
I found that loading two objects with the loadObject method always resulted in an error. However, duplicating the itemProvider with the .copy()
method worked.
For example:
let provider = item.dragItem.itemProvider.copy() as! NSItemProvider
item.dragItem.itemProvider.loadObject(ofClass: URL.self, completionHandler: { (url, _) in
provider.loadObject(ofClass: String.self) { (data, error) in
}
})
Upvotes: 2