Ad

How To Retrieve URL LastPathComponent Of Dropped Document While Using NSItemProviderReading

- 1 answer

iOS 11 comes with Drag & Drop. I want to use it in order to import PDF files to my app. I've created a class that conforms to NSItemProviderReading protocol by implementing object(withItemProviderData:typeIdentifier:) static method and readableTypeIdentifiersForItemProvider property:

import Foundation

@available(iOS 11.0, *)
final class MyDropItem: NSObject, NSItemProviderReading {

    @objc let data: Data
    @objc let typeIdentifier: String

    required init(data: Data, typeIdentifier: String) {
        self.data = data
        self.typeIdentifier = typeIdentifier
        super.init()
    }

    static func object(withItemProviderData data: Data, typeIdentifier: String) throws -> Self {
        return self.init(data: data, typeIdentifier: typeIdentifier)
    }

    static var readableTypeIdentifiersForItemProvider: [String] {
        return [kUTTypePDF as String]
    }

}

However, NSItemProviderReading does not offer a way to import objects from a their original URL. Therefore, I cannot retrieve the URL of the original file and keep some relevant parts such as its lastPathComponent (e.g. the document name MyPDFDocument.pdf).

What should I do in order to be able to create NSItemProviderReading conforming objects while getting their related URLlastPathComponent?

Ad

Answer

I finally discovered that NSItemProvider has a property called suggestedName. suggestedName should be used in order to get the name of a dropped item.

Ad
source: stackoverflow.com
Ad