iOS 11 拖放 UIDocument
iOS 11 Drag and Drop UIDocument
除了如何处理 UIDocuments 之外,关于拖放的一切都清楚了。
这是我的(不工作)实现...
拖动:
func tableView(_ tableView: UITableView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
let doc = DataManager.shared.storage[indexPath.row] // the UIDocument to drag
let itemProv = NSItemProvider()
itemProv.registerFileRepresentation(forTypeIdentifier: "com.mybundle.myapp.myextension",
fileOptions: [.openInPlace],
visibility: .all) { completionHandler in
completionHandler(doc.fileURL, true, nil)
return nil
}
// DragItem
let item = UIDragItem(itemProvider: itemProv)
return [item]
}
掉落:
func tableView(_ tableView: UITableView, performDropWith coordinator: UITableViewDropCoordinator) {
for item in coordinator.session.items {
item.itemProvider.loadFileRepresentation(forTypeIdentifier: "com.mybundle.myapp.myextension", completionHandler: { (url, error) in
if let docUrlOk = url {
debugPrint(urlOk.absoluteString)
DataManager.shared.load(docUrlOk)
}
})
}
}
以及更新方法:
func tableView(_ tableView: UITableView, dropSessionDidUpdate session: UIDropSession, withDestinationIndexPath destinationIndexPath: IndexPath?) -> UITableViewDropProposal {
if tableView.hasActiveDrag {
if session.items.count > 1 {
return UITableViewDropProposal(operation: .cancel)
} else {
return UITableViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
}
} else {
return UITableViewDropProposal(operation: .copy, intent: .insertAtDestinationIndexPath)
}
}
非常感谢
如果你运行代码在iPhone,你需要设置dragItem.enable = YES.
在与 Apple 工程师进行大量交谈后,我找到了 "semi" 解决方案。问题中发布的代码是正确的,但是要将专有文件从您的 App 拖到 Files App,您必须在 Target -> Info -> Exported UTIs 字段中插入以下值
但是如果你将你的专有文件从文件应用程序拖回你的应用程序,UIKit 中有一个错误会阻止它正常工作......工程师告诉我等待 iOS 11.3 或 iOS12,我们拭目以待
除了如何处理 UIDocuments 之外,关于拖放的一切都清楚了。
这是我的(不工作)实现...
拖动:
func tableView(_ tableView: UITableView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
let doc = DataManager.shared.storage[indexPath.row] // the UIDocument to drag
let itemProv = NSItemProvider()
itemProv.registerFileRepresentation(forTypeIdentifier: "com.mybundle.myapp.myextension",
fileOptions: [.openInPlace],
visibility: .all) { completionHandler in
completionHandler(doc.fileURL, true, nil)
return nil
}
// DragItem
let item = UIDragItem(itemProvider: itemProv)
return [item]
}
掉落:
func tableView(_ tableView: UITableView, performDropWith coordinator: UITableViewDropCoordinator) {
for item in coordinator.session.items {
item.itemProvider.loadFileRepresentation(forTypeIdentifier: "com.mybundle.myapp.myextension", completionHandler: { (url, error) in
if let docUrlOk = url {
debugPrint(urlOk.absoluteString)
DataManager.shared.load(docUrlOk)
}
})
}
}
以及更新方法:
func tableView(_ tableView: UITableView, dropSessionDidUpdate session: UIDropSession, withDestinationIndexPath destinationIndexPath: IndexPath?) -> UITableViewDropProposal {
if tableView.hasActiveDrag {
if session.items.count > 1 {
return UITableViewDropProposal(operation: .cancel)
} else {
return UITableViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
}
} else {
return UITableViewDropProposal(operation: .copy, intent: .insertAtDestinationIndexPath)
}
}
非常感谢
如果你运行代码在iPhone,你需要设置dragItem.enable = YES.
在与 Apple 工程师进行大量交谈后,我找到了 "semi" 解决方案。问题中发布的代码是正确的,但是要将专有文件从您的 App 拖到 Files App,您必须在 Target -> Info -> Exported UTIs 字段中插入以下值
但是如果你将你的专有文件从文件应用程序拖回你的应用程序,UIKit 中有一个错误会阻止它正常工作......工程师告诉我等待 iOS 11.3 或 iOS12,我们拭目以待