【发布时间】:2017-09-20 19:02:25
【问题描述】:
有没有人在 iOS 11 中获得自定义文件以拖放?有大量标准图像和文本的示例,但是自定义文件呢?即使是像 PDF 这样的标准文件也会很有用。
【问题讨论】:
有没有人在 iOS 11 中获得自定义文件以拖放?有大量标准图像和文本的示例,但是自定义文件呢?即使是像 PDF 这样的标准文件也会很有用。
【问题讨论】:
创建一个PDFDocument 类并实现NSObject, NSItemProviderReading 协议,然后我们只需在大量示例中将UIImage 更改为PDFDocument 类
更多细节在这里 -> https://stackoverflow.com/a/45982118/3608824
另一种方法是,既然我们有这个kUTTypePDF Uniform Type Identifier,只要你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
}
}
}
【讨论】: