It turns out that the solution is quite simple and is described in session 227 “Drag and Drop Data Delivery” during this WWDC.
Basically you make any object you want to drag according to NSItemProviderWriting , and then implement two things.
NSItemProviderWriting :
An interface to support initialization of a product supplier based on an object used by the source application when providing copied or dragged items.
Step one
Implement writableTypeIdentifiersForItemProviderone that will give your receiver an idea of what type of facility you are providing. This is an array of type identifiers with decreasing precision (they describe it well in the video)
Second step
loadData(withTypeIdentifier typeIdentifier: String, forItemProviderCompletionHandler completionHandler: @escaping (Data?, Error?) -> Void) -> Progress?, , , , .
( firebase), API URL- .
extension Media: NSItemProviderWriting {
static var writableTypeIdentifiersForItemProvider: [String] {
return [(kUTTypeImage as String)]
}
func loadData(withTypeIdentifier typeIdentifier: String, forItemProviderCompletionHandler completionHandler: @escaping (Data?, Error?) -> Void) -> Progress? {
print("Item provider would like to write item from path: \(metadata.path!)")
guard let path = metadata.path else { return nil }
let maxSize:Int64 = (isVideo ? 1000 : 30) * 1024 * 1024
let storage = Storage.storage().reference(withPath: path)
let progress = Progress(totalUnitCount: 100)
var shouldContinue = true
progress.cancellationHandler = {
shouldContinue = false
}
let task = storage.getData(maxSize: maxSize) { data, error in
completionHandler(data, error)
}
if !shouldContinue {
task.cancel()
}
task.observe(.progress) { snapshot in
if let p = snapshot.progress {
progress.completedUnitCount = Int64(p.fractionCompleted * 100)
}
}
task.observe(.success) { snapshot in
print(snapshot)
}
task.observe(.failure) { snapshot in
print(snapshot)
}
return progress
}
}
DragDelegate:
@available(iOS 11, *)
extension GridViewDelegateDataSource: UICollectionViewDragDelegate {
func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
let mediaItem = media[indexPath.item]
let itemProvider = NSItemProvider(object: mediaItem)
let dragItem = UIDragItem(itemProvider: itemProvider)
return [dragItem]
}
}