iOS 11拖放自定义文件?

时间:2017-09-20 19:02:26

标签: ios ipad

有没有人在iOS 11中拖放自定义文件?有大量的标准图像和文本示例,但自定义文件呢?即使像PDF这样的标准文件也很有用。

1 个答案:

答案 0 :(得分:1)

  • 方法1:

制作PDFDocument课程并实施NSObject, NSItemProviderReading协议,然后我们只需将UIImage更改为PDFDocument

此处提供更多详细信息 - > https://stackoverflow.com/a/45982118/3608824

  • 方法2:

另一种方法是,因为我们有kUTTypePDF统一类型标识符,只要您import MobileCoreServices,根据dropInteraction(_:performDrop:)文档,我们可以使用此函数loadFileRepresentation(forTypeIdentifier:completionHandler:),这样应该有效:

func dropInteraction(_ interaction: UIDropInteraction, canHandle session: UIDropSession) -> Bool {
    return session.hasItemsConforming(toTypeIdentifiers: [kUTTypePDF as String]) && session.items.count == 1
}

func dropInteraction(_ interaction: UIDropInteraction, sessionDidUpdate session: UIDropSession) -> UIDropProposal {
    return UIDropProposal(operation: .copy)
}

func dropInteraction(_ interaction: UIDropInteraction, performDrop session: UIDropSession) {
    if let itemProvider = (session.items.first)?.itemProvider {
        itemProvider.loadDataRepresentation(forTypeIdentifier: kUTTypePDF as String) { (data, error) in
            // do the work
        }
    }

}