簡體   English   中英

iOS 11拖放自定義文件?

[英]iOS 11 drag and drop custom files?

有沒有人在iOS 11中拖放自定義文件? 有大量的標准圖像和文本示例,但自定義文件呢? 即使像PDF這樣的標准文件也很有用。

  • 方法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
        }
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM