【问题标题】:iOS 11 drag and drop custom files?iOS 11 拖放自定义文件?
【发布时间】:2017-09-20 19:02:25
【问题描述】:

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

【问题讨论】:

    标签: ios ipad


    【解决方案1】:
    • 方法一:

    创建一个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
            }
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2018-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-08
      • 2019-06-10
      • 1970-01-01
      相关资源
      最近更新 更多