有没有人在iOS 11中拖放自定义文件?有大量的标准图像和文本示例,但自定义文件呢?即使像PDF这样的标准文件也很有用。
答案 0 :(得分:1)
制作PDFDocument
课程并实施NSObject, NSItemProviderReading
协议,然后我们只需将UIImage
更改为PDFDocument
类
此处提供更多详细信息 - > https://stackoverflow.com/a/45982118/3608824
另一种方法是,因为我们有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
}
}
}